【问题标题】:How can you directly output the result of a helper that takes a block in ERB?如何直接输出在 ERB 中获取块的助手的结果?
【发布时间】:2011-03-24 16:35:34
【问题描述】:

我有一个带块的助手:

def container(&block)
  render(:partial => 'layouts/container', :locals => {:content => capture(&block)})
end

当我尝试在 ERB 的 <%= ... %> 标记内使用它时:

<%= container do %>
  Test
<% end %>

我收到compile error

compile error
test2.html.erb:1: syntax error, unexpected ')'
            old_output_buffer = output_buffer;;@output_buffer = '';  __in_erb_template=true ; @output_buffer.concat(( container do ).to_s); @output_buffer.concat "\r\n"
                                                                                                                                    ^
test2.html.erb:4: syntax error, unexpected kENSURE, expecting ')'
test2.html.erb:6: syntax error, unexpected kEND, expecting ')'

但是,如果我捕获助手的输出然后输出它:

<% output = container do %>
  Test
<% end %>
<%= output %>

它工作正常,但很丑。

有什么方法可以做我想做的事吗? (注意:通常我使用 HAML,但我试图让我的助手为团队中目前仍在 ERB 工作的其他人工作;切换到 HAML 不是解决方案)。

【问题讨论】:

    标签: ruby-on-rails erb block


    【解决方案1】:

    与其把别人的功劳归于别人,我只会指点你here.

    【讨论】:

    • 这并不能真正解决我的问题。我不想更改助手的工作方式,因为我希望它也能继续在 HAML 中工作。另外,该项目使用 Rails,所以我不会像那样直接调用 ERB。
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多