This extension is part of the Sinatra::Contrib project. Run gem install sinatra-contrib to have it available.
Sinatra::ContentFor is a set of helpers that allows you to capture blocks inside views to be rendered later during the request. The most common use is to populate different parts of your layout from your view.
The currently supported engines are: Erb, Erubis, Haml and Slim.
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb <% content_for :some_key do %> <chunk of="html">...</chunk> <% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb <%= yield_content :some_key %>
To use the helpers in a classic application all you need to do is require them:
require "sinatra" require "sinatra/content_for" # Your classic application code goes here...
To use the helpers in a modular application you need to require them, and then, tell the application you will use them:
require "sinatra/base" require "sinatra/content_for" class MyApp < Sinatra::Base helpers Sinatra::ContentFor # The rest of your modular application code goes here... end
And How Is This Useful?