2008年8月6日 星期三

DRY in views, view的外圍是重覆的, 只有內部content 不同, 把內部content 當作block

DRY in view
http://www.igvita.com/2007/03/15/block-helpers-and-dry-views-in-rails/
Block to partial
http://errtheblog.com/posts/11-block-to-partial
binding
http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc/style/print
concat
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001727
capture
http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M001750




[view]
<% round 'title' do %>
I be rounded
<% end %>

[helper]
def round(name, &block)
option = {}
option.merge!(:name => name, :body => capture(&block))
concat(render(:partial => 'round_out', :locals => option), block.binding)
end

[partial]
<div>
<b>aaaa</b>
<div>
<%= name %>
</div>
<div>
<%= body %>
</div>
<b>bbbb</b>
</div>

沒有留言: