var = #{message}
in my example I get undefined local variable or method message
The haml documentation for filters states that you can interpolate Ruby code using #{}
- flavor = "raspberry"
#content
:textile
I *really* prefer _#{h flavor}_ jam.
First, let's review what you seem to know:
- ...
.#{...}
markup to interpolate Ruby code inside a filter.You say you want to run each
, but presumably you want output from this; since the result of #{...}
is turned into a string and put in your code, what you really want (probably) is map
:
%html
%head
:javascript
var foo = [];
#{
limit = rand(4)+3
array = (0..limit).to_a
array.map{ |i| "foo[#{i}] = #{rand(12)};" }.join ' '
}
console.log(foo.length);
%body
Running the above code gives this output:
<html>
<head>
<script type='text/javascript'>
//<![CDATA[
var foo = [];
foo[0] = 2; foo[1] = 0; foo[2] = 11; foo[3] = 8; foo[4] = 0; foo[5] = 1;
//]]>
</script>
<body></body>
</head>
</html>
As you can see, the big #{...}
block (which may span multiple lines) runs arbitrary Ruby code. The result of the last expression (in this case the map{...}.join
) is converted to a string and placed in the output.