typ*_*ror 4 ruby haml middleman
当我使用:ruby过滤器在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
Run Code Online (Sandbox Code Playgroud)
创建一个名为 的 IO 对象
haml_io,写入它的任何内容都会输出到 Haml 文档中。
究竟如何使用haml_io 对象写入 haml 文档,而不是写入控制台(认为我需要除 之外的其他东西puts)?
这种行为最近发生了变化——旧的行为(4.0 版之前)是将任何写入标准的内容写入 Haml 文档,但这不是线程安全的。
haml_io是一个局部变量,指的是写入文档的 IO 对象。您为使用它而重写的代码看起来像这样(假设comments,max_comments并且li_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
Run Code Online (Sandbox Code Playgroud)
haml_io实际上是一个StringIO对象,因此您可以使用它的任何方法,例如haml_io.write,haml_io.putc它将输出重定向到您的文档。
| 归档时间: |
|
| 查看次数: |
843 次 |
| 最近记录: |