rails html helper

dev*_*vth 6 haml ruby-on-rails partials

我试图弄清楚在其中生成一些html和嵌套内容的最简洁方法.使用HAML.基本上我想做的事情如下:

= block_large
  This is some nested content
Run Code Online (Sandbox Code Playgroud)

这应该产生:

<div class="block large">
  <img src="block_large_carat.gif" class="block_large_carat">
  This is some nested content
</div>
Run Code Online (Sandbox Code Playgroud)

问题是我不知道如何实现这一目标.谐音?帮手?我对如何嵌套我想要的任何内容感到困惑.试图保持我的HAML DRY并且不想一遍又一遍地明确声明图像标签.

Car*_*ima 5

编辑:
我以前的解决方案不起作用:)
感谢EmFi指出它.
这次我(甚至)测试了它,它(甚至)工作了!\ O /

我是根据这篇博文发布的.
阅读完整的帖子以获得更好的解释:)

应用程序/佣工/ application_helper.rb

  def block_to_partial(partial_name, options = {}, &block)
    options.merge!(:body => capture(&block))
    concat(render(:partial => partial_name, :locals => options), block.binding)
  end
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/ XXX/new.html.haml

%h2 Test!
- block_to_partial("block_large", :class_name=>"nested_content") do
  This is some nested content
OOps..
Run Code Online (Sandbox Code Playgroud)

应用程序/视图/ XXX/_block_large.html.haml

#block_large
  %img(src="block_large_carat.gif" class="block_large_carat")
  %div(class=class_name)
    = body
Run Code Online (Sandbox Code Playgroud)

呈现:

<div id='block_large'>
  <img class='block_large_carat' src='block_large_carat.gif' />
  <div class='nested_content'>
    This is some nested content
  </div>
</div>
OOps..
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!