Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slim-lang's if-else nested code

Tags:

I want to assign a css class to a part of HTML based on condition in slim-lang

I am doing the following

- if current_user   .title - else   .title_else 

Now how can i write the HTML that should be nested in the one of the class above? Do i need to write HTML in the both if condition and else condition ?

The reason is that the HTML that should be nested in one of the above class should be further intended rightwards. But if i intend it further right, it is including under else condition. How shoule i approach this now ?

like image 491
nik7 Avatar asked Dec 20 '12 07:12

nik7


2 Answers

Here's a one liner:

.thing class=(current_user ? 'large-menu' : 'medium-menu')   h4 Same content, different class 
like image 98
Tiago Franco Avatar answered Oct 02 '22 02:10

Tiago Franco


Since you're just toggling between two attributes, Tiago Franco's answer is certainly sufficient for your purposes. However, there is another way that comes in handy.

You can use capture to render HTML to a local variable, and then embed that content inside whatever control structure you like. So, if your wrapping content was a bit more complex, you'd do this:

- shared_content = capture do   div This is displayed regardless of the wrapper - if current_user   | You're logged in!   .title     = shared_content - else   | You're not logged in!   .title_else     = shared_content 

I'll often use this to wrap content in a link if there's a URL available:

- image = capture do   img src=image_url - if url.blank?   = image - else   a href=url = image   
like image 26
Craig Walker Avatar answered Oct 02 '22 01:10

Craig Walker