Ruby block code in haml
Asked Answered
T

3

9

The code below is working

[email protected] do |item|
 %tr
  %td
   =item.quantity
   times
  %td= item.product.title
 end

However I want to use { } instead of do ... end. Is it possible?

[email protected] { |item|
 %tr
  %td
   =item.quantity
   times
  %td= item.product.title
 }

For some reason it throws an exception. What did I do wrong?

Terrilynterrine answered 7/8, 2012 at 8:46 Comment(1)
You can't. That's now how HAML works.Doublequick
R
15

In your working example the end line is being treated as raw text, and will be included in your output. This works because you effectively don’t have an end keyword. If you change this line to - end — i.e. add the hyphen (and make sure you’ve got your indentation correct) — you’ll see an error like:

Syntax error on line 9: You don't need to use "- end" in Haml. Un-indent to close a block:
- if foo?
  %strong Foo!
- else
  Not foo.
%p This line is un-indented, so it isn't part of the "if" block

Note that Haml doesn’t always disallow end keywords, only end by itself. You can use them with statement modifiers, e.g.:

- @things.each do |thing|
  %p= thing.to_s
-end unless @dont_show_things

or if you want to make use of the result of the block:

= @things.map do |thing|
  - do_something_with(thing)
-end.join(",")

As for using {}, you can’t. When Haml compiles the template to Ruby the hardcoded string end is added to close any blocks as needed. It could be possible to modify Haml to be able to use do..end or {}, but the extra complexity probably isn’t worth it. This way is a better fit with the common Ruby idiom of using do...end for multiline blocks, and {} for single line ones.

If you need to use {} because of some issue with operator precedence it might be a sign to create a helper method. Your Haml templates (or templates in any language) aren’t the best place for complex logic, it’s best to move it into real Ruby files.

Rockaway answered 10/8, 2012 at 21:45 Comment(1)
For anyone still using haml :), I was trying to get map/join(",") working to output some markup formatted within the map block and it wasn't working as expected. To get things working I had to do: gist.github.com/bradley/21a7b087d5b2756dc0f90af9cecce780Ripply
B
5

You DO NOT put any end in HAML. The indentation is semantic. Your code should therefore be :

- @items.each do |item|
  %tr
    %td
      = item.quantity
      times
    %td= item.product.title

EDIT: Using { will throw a syntax error.

Bloomer answered 7/8, 2012 at 8:49 Comment(4)
I'm asking about {} but about do ... end.Terrilynterrine
I know that using end is redundant. But it doesn't throw an error.Terrilynterrine
It doesn't throw an error because it's considered as text. Evaluating your code will actually display "end" in the HTML code.Bloomer
I don't know why this was voted down, the advice is correct. I've voted it up.Hamforrd
P
3

If you want to add complex ruby code, you can use the ruby syntax,

:ruby
  x = case a
  when 2
     "two"
  when 3
     "three"
  end
Pain answered 3/2, 2023 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.