6

私は、HTMLを少し生成し、その中にコンテンツをネストする最もクリーンな方法を見つけようとしています。HAMLを使用します。基本的に私は次のようなことをしたいです:

= block_large
  This is some nested content

そしてそれは生成するはずです:

<div class="block large">
  <img src="block_large_carat.gif" class="block_large_carat">
  This is some nested content
</div>

問題は、これを達成する方法がわからないことです。パーシャル?ヘルパー?必要なコンテンツをどのようにネストするかについて、私は夢中になっています。HAML DRYを維持しようとしていて、イメージタグを何度も明示的に宣言する必要がないようにします。

4

1 に答える 1

5

編集:
私の以前の解決策は機能しませんでした:)
それを指摘してくれたEmFiに感謝します。
今回、私はそれを(さえ)テストしました、そしてそれは(さえ)働きました!\ o /

このブログ投稿に基づいて、これをここに投稿しています。
はるかに良い説明のために完全な投稿を読んでください:)

app / helpers / 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

app / views / xxx / new.html.haml

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

app / views / xxx / _block_large.html.haml

#block_large
  %img(src="block_large_carat.gif" class="block_large_carat")
  %div(class=class_name)
    = body

レンダリング:

<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..

お役に立てば幸いです。

于 2009-11-15T03:53:03.267 に答える