How can I use multiple lines for a single Ruby statement in HAML? For example, I'd like to write something like the following:
- entries = [{
- :title => "The Fellowship of the Ring",
- :body => "We should walk instead of fly"
- }]
!!! 5
%html
%head
%title Blog
%body
#content
- entries.each do |entry|
.entry
%h3.title= entry[:title]
%p.body= entry[:body]
But of course the first four lines produce a syntax error.
According to the HAML FAQ:
[W]hen a function has lots of arguments, it’s possible to wrap it across multiple lines as long as each line ends in a comma.
So, the following should be valid HAML:
- entries = [{ :title => "The Fellowship of the Ring", :body => "We should walk instead of fly" }]
You can use :ruby
filter.
:ruby entries = [{ :title => "The Fellowship of the Ring", :body => "We should walk instead of fly" }] !!! 5 %html %head %title Blog %body #content - entries.each do |entry| .entry %h3.title= entry[:title] %p.body= entry[:body]
Check out the docs here.
Note that it's intentionally awkward because you're not supposed to be putting lots of ruby in your templates.
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