No commit activity in last 3 years
No release in over 3 years
Small Sinatra extension to add a content_for helper similar to Rails'
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.0.10
>= 0
>= 0
!= 0.9.0
>= 0
>= 0
>= 0

Runtime

 Project Readme

ContentFor2¶ ↑

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.

When using this with the Haml rendering engine, you should do the following:

- content_for :some_key do
  %chunk{ :of => "html" } ...

= yield_content :some_key

Note that with ERB yield_content is called without an ‘=’ block (<%= %>), but with Haml it uses = yield_content.

Using an ‘=’ block in ERB will output the content twice for each block, so if you have problems with that, make sure to check for this.

Note Use <%= yield_content ... %> in Erubis.

Also we provide content_for? method that checks that block is defined.

Usage¶ ↑

If you’re writing “classic” style apps, then requring sinatra/content_for2 should be enough. If you’re writing “classy” apps, then you also need to call helpers Sinatra::ContentFor2 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.

Credits¶ ↑

Code by foca, inspired on the Ruby on Rails helpers with the same name. Haml support by mattly. Erubis support by hiroyuki. Bundler/etc. unicode compatibility by arnabc and akzhan. content_for? method by BrunoGrasselli.