When I use the :ruby
filter to do some simple stuff in haml, for example...
:ruby
to = comments > max_comments ? max_comments : comments
(0...to).each do |i|
comment = data[i]
puts li_comment comment[0], comment[1], comment[2]
end
the puts
statement writes the output to the console. The docs for :ruby indicate that it
Creates an IO object named
haml_io
, anything written to it is output into the Haml document.
How exactly does one use the haml_io object to write to the haml document, but not to the console (think I need something other than puts
)?
This behaviour changed recently – the old behaviour (before version 4.0) was to write anything written to standard out to the Haml document, but this wasn’t thread safe.
haml_io
is a local variable that refers to an IO object that writes to the document. Your code rewritten to use it would look something like this (assuming comments
, max_comments
and li_comment
are all defined earlier):
:ruby
to = comments > max_comments ? max_comments : comments
(0...to).each do |i|
comment = data[i]
haml_io.puts li_comment comment[0], comment[1], comment[2]
end
haml_io
is actually a StringIO
object so you can use any of its methods, e.g. haml_io.write
, haml_io.putc
and it will redirect output to your document.