user569825
user569825

Reputation: 2459

How to use line breaks in a slim variable?

I have this variable in slim:

- foo = 'my \n desired multiline <br/> string'
#{foo}

When I parse the output using the slimrb command line command the contents of the variable are encoded:

my \n desired multiline &lt;br/&gt; string

How can I have slimrb output the raw contents in order to generate multi-line strings?

Note that neither .html_safe nor .raw are available.

Upvotes: 2

Views: 3446

Answers (2)

Olalekan Sogunle
Olalekan Sogunle

Reputation: 2357

An easier way is to use Line Indicators as verbatim texts | . Documentation here . For example;

p
 | This line is on the left margin.
   This line will have one space in front of it.

Upvotes: 2

matt
matt

Reputation: 79783

There are two issues here. First in Ruby strings using single quotes – ' – don’t convert \n to newlines, they remain as literal \ and n. You need to use double quotes. This applies to Slim too.

Second, Slim HTML escapes the result of interpolation by default. To avoid this use double braces around the code. Slim also HTML escapes Ruby output by default (using =). To avoid escaping in that case use double equals (==).

Combining these two, your code will look something like:

- foo = "my \n desired multiline <br/> string"
td #{{foo}}

This produces:

<td>my
 desired multiline <br/> string</td>

Upvotes: 4

Related Questions