Search code examples
rubyhamlerbslim-lang

How to inject two variables next to each other separated with a space?


The expected HTML result is as follows:

<li>description1 name1</li>
<li>description2 name2</li>
<!-- ... -->

Where the list of description-name is known and can be iterated over.

I tried to do:

li
  = tool.description
  | &nbsp;
  = tool.name

or

li
  = "#{tool.description}&nbsp;#{tool.name}"

but it seems like an ugly way to achieve that.

Is there any other and elegant solution?


Solution

  • You can use interpolation directly in both Slim and Haml, so you don’t need to use = and quote the whole string.

    In Slim, you could do:

    li #{tool.description}&nbsp;#{tool.name}
    

    and in Haml the only difference is you just need to add the lead %:

    %li #{tool.description}&nbsp;#{tool.name}