foca / sinatra-content-for

Sinatra extension to use a `content_for` helper similar to Rails'

This URL has Read+Write access

name age message
file .gitignore Thu May 07 06:58:41 -0700 2009 Gemspec, Rakefile, etc [foca]
file LICENSE Wed May 06 22:58:50 -0700 2009 Initial commit [foca]
file README.rdoc Loading commit data...
file Rakefile Thu May 07 06:58:41 -0700 2009 Gemspec, Rakefile, etc [foca]
directory lib/ Sat May 09 10:19:25 -0700 2009 Add Haml support [mattly]
file sinatra-content-for.gemspec Sat May 09 10:21:13 -0700 2009 Push version to 0.2 This release adds: * Suppo... [foca]
directory test/ Sat May 09 10:19:25 -0700 2009 Add Haml support [mattly]
README.rdoc

ContentFor

Small extension for the Sinatra web framework that allows you to use the following helpers in your views:

    <% content_for :some_key do %>
      <chunk of="html">...</chunk>
    <% end %>

    <% yield_content :some_key %>

This allows you to capture blocks inside views to be rendered later in this request. For example, to populate different parts of your layout from your view.

Usage

If you’re writing "classic" style apps, then requring sinatra/content_for should be enough. If you’re writing "classy" apps, then you also need to call helpers Sinatra::ContentFor in your app definition.

And how is this useful?

For example, some of your views might need a few javascript tags and stylesheets, but you don’t want to force this files in all your pages. Then you can put <% yield_content :scripts_and_styles %> on your layout, inside the <head> tag, and each view can call content_for setting the appropriate set of tags that should be added to the layout.

Warning

This only works with ERB as a rendering mechanism. I haven’t figured how to make it work with Haml. If you find a way, contact me and I’ll include it.

Credits

Code by foca, inspired on the Ruby on Rails helpers with the same name.