Ruby code embedded in Haml?

C

cz

Hi:

Isit possible to embed ruby code in Haml templates. True, using
variables seems to work fine. However, even a simple for loop
generates errors. Other members have made suggestions, but none of
them have worked. Below is a simple Haml template. When it is used in
conjunction witha a Haml layout, an Html page is displayed. Four
indexes of an array are displayed. How could a for loop accomplish the
same thing? Thank you much.

cz

#contentID
%h3 Haml Template
%p All work and no play makes Jack a dull boy.
.divbtm
%p #{@ary.at(0)}
%p #{@ary.at(1)}
%p #{@ary.at(2)}
%p #{@ary.at(3)}
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Hi:

Isit possible to embed ruby code in Haml templates. True, using
variables seems to work fine. However, even a simple for loop
generates errors. Other members have made suggestions, but none of
them have worked. Below is a simple Haml template. When it is used in
conjunction witha a Haml layout, an Html page is displayed. Four
indexes of an array are displayed. How could a for loop accomplish the
same thing? Thank you much.

cz

#contentID
%h3 Haml Template
%p All work and no play makes Jack a dull boy.
.divbtm
%p #{@ary.at(0)}
%p #{@ary.at(1)}
%p #{@ary.at(2)}
%p #{@ary.at(3)}
Dashes are for zero-width ruby, such as control characters, and equal signs
will embed Ruby, ie interpolation.

- @ary.each do |element|
%p= element
 
B

Brian Candler

Josh Cheek wrote in post #982617:
Dashes are for zero-width ruby, such as control characters, and equal
signs
will embed Ruby, ie interpolation.

Or perhaps more clearly: a dash is for ruby code where you don't want
the result value inserted into the page, and equals is for ruby code
where the value of the expression is inserted into the page.
- @ary.each do |element|
%p= element

To be clear, this is a shorthand in haml for

- @ary.each do |element|
%p
= element
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top