by ampatspell
in Code
A lot of times it’s very convinient to use ruby blocks in Webby pages. For example this HTML:
<div class="container clearfix"> <div class="left">Title</div> <div class="right"> <p>Some random content</p> </div> </div>
…can be created using simple ruby helper what takes block and title parameter like this:
<% container("Title") do %>
<p>Some random content</p>
<% end %>
To implement it we need to capure_erb and concat_erb. Not a good idea to use those methods in every erb helper, so I made with_content_of method what wraps those creepy things:
def with_content_of(content_block, &block) content = capture_erb(&content_block) concat_erb(yield(content), content_block.binding) end
Now composite can be implemented as following:
# takes a block with some random text/html content from erb def composite(title, &block) # call helper, give 'em that block and get back the content () with_content_of block do |content| # use the content from erb block and do with it whatever you like render :partial => "composite", :locals => { :left => title, :right => content } end end