ヘッダー、サブヘッダー、フッター、本文、ID などの可変部分を持つ多くのモジュールを含むページがあります。まず、レイアウトを作成し、ローカル変数とメイン ブロックを使用してレイアウトをレンダリングしてテンプレートを埋めました。
レイアウト:
.container{:id => local_assigns[:id].present? ? id : nil}
.header
= title
= subtitle
.body
= yield
.footer
= footer
意見:
= render :layout => "shared/example", :locals => {:title => 'Heading', :subtitle => 'Sub heading', :footer => 'closing part'} do
body of the template
これはうまく機能しますが、ページ内にこのレイアウトのインスタンスが複数あることを念頭に置いて、サブタイトルとフッターにブロックを渡したいと思います。これまでのところ、これを解決できた唯一の方法は、capture_haml
ヘルパーを使用することです
Haml をローカル変数として表示:
- exampleSubtitle = capture_haml do
%h3 subtitle
%button action
= render :layout => "shared/example", :locals => {:title => 'Heading', :subtitle => exampleSubtitle, :footer => 'closing part'} do
body of the template
この例は、質問を明確にするために少し簡略化されています。:locals
実際には、ハッシュが「Haml にとって醜い」サイズに成長することを心配しており、可能であればそれを防ぎたいと考えています。
物事をきれいに保つために、コンテキスト コンテンツ ブロックを定義する方法はありますか?
希望的観測:
= render :layout => "shared/example", :locals => {:title => 'Heading'} do
- content_for :subtitle do
%h3 subtitle
%button action
- content_for :footer do
%a{href => "#top"} top
body of the template
希望に満ちたレイアウト:
.container{:id => local_assigns[:id].present? ? id : nil}
.header
= title
- if content_for?(:subtitle)
= yield :subtitle
.body
= yield
- if content_for?(:footer)
.footer
= yield :footer