I use something similar in my templates. Here is a modification. This should work in Rails 3.
application_helper.rb:
module ApplicationHelper class PageBuilder def initialize(title, template) @title, @template = title, template @header, @body, @sidebar = nil, nil, nil @options = { :page => {} , :header => {}, :sidebar => {}, :body => {}, :title => {} } @logger = Rails.logger end def parse(&block) if block_given? if @template.respond_to?(:is_haml?) && @template.is_haml? contents = @template.capture_haml(&block) else
Now, in your code example, when is page.has_sidebar? == false page.has_sidebar? == false , you will get
<div class="page"><title>Super Cool Page</title><header><h1> Ruby is Cool </h1></header><section> Witty discourse on Ruby. </section></div>
and when page.has_sidebar? == true page.has_sidebar? == true , you will get
<div class="page"><title>Super Cool Page</title><header><h1> Ruby is Cool </h1></header><asside> <ul><li>Option 1</li></ul> </asside><section> Witty discourse on Ruby. </section></div>
You can change the order of things in the page method to get the desired layout as output.
source share