Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3876146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:20:53+00:00 2026-05-19T22:20:53+00:00

This is how I’d like to write markup in say index.html.erb <%= page_for Super

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:20:54+00:00Added an answer on May 19, 2026 at 10:20 pm

    I use something similar in my templates. Here’s 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
              #erb
              contents = @template.capture(&block)
            end
          else
            contents = ""
          end
          contents
        end
    
        def page (options,&block)
          options[:class] ||= "page"
          @options[:page] = options
          parse(&block)
          content = ""
          content += @template.content_tag(:title, @options[:title]) { @title } unless @title.nil?
          content += @template.content_tag(:header,@options[:header]) do
            @template.content_tag( :h1) { @header } 
          end unless @header.nil?
          content += @template.content_tag(:asside, @options[:sidebar]) { @sidebar } unless @sidebar.nil?
          content += @template.content_tag(:section, @options[:section]) { @body } unless @body.nil?
          return @template.content_tag(:div, @options[:page]) { content.html_safe }
        end
        def header(options={},&block)
          @options[:header] = options
          @header = parse(&block)
          nil
        end
        def sidebar(options={},&block)
          @options[:sidebar] = options
          @sidebar = parse(&block)
          nil
        end
        def body(options={},&block)
          @options[:body] = options
          @body = parse(&block)
          nil
        end
      end
    
      def page_for(title, options = {}, &block )
        raise ArgumentError, "Missing block" unless block_given?
        builder = PageBuilder.new(title, view_context )
        return builder.page(options) do 
          block.call(builder)
        end
      end
    end
    

    Now, in your sample code, when 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, 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 rearrange stuff in the page method to get any desired layout as the output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This might seem like a stupid question I admit. But I'm in a small
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
This might seem like a basic question and back to Http protocol 101. But
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have some data like this: 1 2 3 4 5 9 2 6
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.