Ruby 部分块渲染乱序
Ruby partial block render out of order
我在模板中定义一个块并将其传递到部分并将其呈现为字符串,但是在部分内部调用块似乎呈现到模板上下文中?
lib/test.rb
class Test
def render(&block)
view = ApplicationController.new()
view.class_eval do
include ApplicationHelper
end
view.render_to_string(:partial => 'cool_partial', :locals => {
:block => block
})
end
end
controller/action.haml
= @test_obj.render do |variable|
%td='hello-world'
app/view/_cool_partial.haml
%table
%tr
(0..5).each do |i|
block.call
输出
<td>hello-world</td>
<td>hello-world</td>
<td>hello-world</td>
<td>hello-world</td>
<td>hello-world</td>
<table>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
这实际上是由于 HAML 内部的一个问题:
http://haml.info/docs/yardoc/Haml/Engine.html
Due to some Ruby quirks, if scope is a Binding or Proc object and a block is given, the evaluation context may not be quite what the user expects.
不过,这可以通过 capture_haml
助手解决!
= capture_haml &row_block
你仍然可以传递参数,
= capture_haml arg1, arg2, &row_block
我在模板中定义一个块并将其传递到部分并将其呈现为字符串,但是在部分内部调用块似乎呈现到模板上下文中?
lib/test.rb
class Test
def render(&block)
view = ApplicationController.new()
view.class_eval do
include ApplicationHelper
end
view.render_to_string(:partial => 'cool_partial', :locals => {
:block => block
})
end
end
controller/action.haml
= @test_obj.render do |variable|
%td='hello-world'
app/view/_cool_partial.haml
%table
%tr
(0..5).each do |i|
block.call
输出
<td>hello-world</td>
<td>hello-world</td>
<td>hello-world</td>
<td>hello-world</td>
<td>hello-world</td>
<table>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
</table>
这实际上是由于 HAML 内部的一个问题: http://haml.info/docs/yardoc/Haml/Engine.html
Due to some Ruby quirks, if scope is a Binding or Proc object and a block is given, the evaluation context may not be quite what the user expects.
不过,这可以通过 capture_haml
助手解决!
= capture_haml &row_block
你仍然可以传递参数,
= capture_haml arg1, arg2, &row_block