How can I create specialized builders for semantic layout in rails?
- by Paul Alexander
This is how I'd like to write markup in say index.html.erb
<%= page_for "Super Cool Page" do |p| %>
<%= p.header do %>
Ruby is Cool
<% end %>
<%= p.body do %>
Witty discourse on Ruby.
<% end %>
<% if page.has_sidebar? %>
<%= p.sidebar do %>
<ul><li>Option 1</li></ul>
<% end %>
<% end %>
<% end %>
Which would output
<div class="page">
<header><h1>Super Cool Page</h1></header>
<section>
Witty discourse on Ruby.
</section>
</div>
and when page.has_sidebar? is true
<div class="page">
<header><h1>Super Cool Page</h1></header>
<asside><ul><li>Option 1</li></ul></asside>
<section>
Witty discourse on Ruby.
</section>
</div>
I've taken a look at the FormHelper class in rails for guidance, but it seems like I'd have to duplicate a lot of work which I'm trying to avoid. I'm really just trying to figure out where to hang the classes/modules/methods in the framework and whit kind of object |p| should be.
My first inclination was to create a PageBuilder class that implements header, body and sidebar methods. But I got stuck on the rendering pipeline to get everything output just right.
Is there a gem that already provides this type of semantic generation? If not I'd love any insight on how to set this up.