So I am a newbie at HAML.
While going through the HAML tutorial the way to represent ruby code was mentioned as =
eg: %strong= item.title
but when I ran this code:
= @list.documents.each do |doc|
%tbody
%tr
%td= doc.display_name
along with all the list data which was displayed there was also alot of junk data displayed which was related to the actual list data which was displayed. This is what I got:
val1 val2 val3 [#Document......@id : val1, @id:val2.....]
When i try the same code while replacing the =
with a -
the unwanted data is not received.
- @list.documents.each do |doc|
%tbody
%tr
%td= doc.display_name
output:
val1 val2 val3
can someone explain the difference between -
and =
while writing ruby code in haml?
They are both indicators that what follows needs to be processed as Ruby code. But -
doesn't output the result to view whereas =
does.
For example, consider the following helper:
def hlp
[1,2].each(&:succ) # using example relevant to your code sample
end
each
will return the enumerator that it is called upon. So the return value of that helper method will be [1,2]
.
The following in your HAML view will not display [1,2]
:
- hlp
But the following will display it:
= hlp
Therefore in the sample code you have shared, you will use -
, and not =
, because you don't want to display all @list.documents
once each
is done with them.
In haml=
is used to write ruby code and displays the result as output
Where as -
is used to write ruby code within haml and no output is diplayed.
You can read more about haml from this
http://haml.info/tutorial.html
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