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 ?
Here's a one liner:
.thing class=(current_user ? 'large-menu' : 'medium-menu') h4 Same content, different class
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With