For those coming to the Slim template engine from other tools, or from nothing at all in this area, getting acquainted with Slim can prove daunting — it did for me!
Fortunately, it’s the kind of syntactic methodology that succumbs quickly to a little hard and fast learning. Personally, I ran across a few “gotchas” that I want to share lest you run across them yourself. Knowing these beforehand will save you time, effort, and frustration.
Inline conditionals
Getting logic on the same line as an element can be tricky, especially if it’s more than one straightforward directive. For conditionals, you can employ the ternary operator. Here’s how to do that with Slim:
.item class=(condition ? 'outcome-1-class' : 'outcome-2-class')
.subitem
Obviously this method can be applied to more than just classes.
See the original solution on StackOverflow.
HTML from a string
Slim allows you to embed plain HTML directly inside its own syntax. If your code is coming from an external source in the form of a string, however, it won’t render, due to Rails treating the string as potentially malicious, and escaped. You can avoid this simply by running it through html_safe, as:
codeString.html_safe
Text inside a tag
Setting up the alignment of your content just right so that Slim recognizes your intent is no simple feat for the uninitiated. Placing nested or complex content in a link tag in particular gave me serious difficulty.
The basic outline goes something like this:
a href="location" span class="item" | Something to say span class="item-other"
| Something else to say
See the original on StackOverflow.
As usual, riff on this as you require.