2013-06-26 55 views
3

当我使用:ruby过滤器做一些简单的东西在HAML,例如...输出HAML内容:红宝石滤波

: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 

puts语句写入输出到控制台。该docs for :ruby表明它

创建任何书面向名为haml_io一个IO对象,是输出 到Haml的文件。

究竟怎样才能使用的haml_io对象写入文件HAML,而不是控制台(觉得我需要比puts其他东西)?

回答

4

此行为changed recently - 旧行为(版本4.0之前)将任何写入标准的任何内容写入Haml文档,但这不是线程安全的。

haml_iolocal variable that refers to an IO object that writes to the document。您的代码重写,使用它会是这个样子(假设commentsmax_commentsli_comment都是早先定义):

: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实际上是一个StringIO object所以你可以使用它的任何方法,例如haml_io.write,haml_io.putc它会将输出重定向到您的文档。

0

...打电话haml_io

写入它的任何内容都会输出到Haml文档中。