Module: Sinatra::ContentFor
- Includes:
- Capture
- Defined in:
- lib/sinatra/content_for.rb
Overview
Sinatra::ContentFor
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.
Usage
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 %>
Classic Application
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...
Modular Application
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
register Sinatra::ContentFor
# The rest of your modular application code goes here...
end
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.
Constant Summary
Constants included from Capture
Instance Attribute Summary
Attributes included from EngineTracking
Instance Method Summary collapse
-
#content_for(key, &block) ⇒ Object
Capture a block of content to be rendered later.
-
#yield_content(key, *args) ⇒ Object
Render the captured blocks for a given key.
Methods included from Capture
Methods included from EngineTracking
#builder?, #coffee?, #creole?, #erb?, #erubis?, #haml?, #initialize, #less?, #liquid?, #markaby?, #markdown?, #nokogiri?, #radius?, #rdoc?, #sass?, #scss?, #slim?, #textile?, #with_engine
Instance Method Details
#content_for(key, &block) ⇒ Object
Capture a block of content to be rendered later. For example:
<% content_for :head do %>
<script type="text/javascript" src="/foo.js"></script>
<% end %>
You can call content_for
multiple times with the same key (in the example :head
), and when you render the blocks for that key all of them will be rendered, in the same order you captured them.
Your blocks can also receive values, which are passed to them by yield_content
78 79 80 |
# File 'lib/sinatra/content_for.rb', line 78 def content_for(key, &block) content_blocks[key.to_sym] << capture_later(&block) end |
#yield_content(key, *args) ⇒ Object
Render the captured blocks for a given key. For example:
<head>
<title>Example</title>
<%= yield_content :head %>
</head>
Would render everything you declared with content_for :head
before closing the <head>
tag.
You can also pass values to the content blocks by passing them as arguments after the key:
<%= yield_content :head, 1, 2 %>
Would pass 1
and 2
to all the blocks registered for :head
.
99 100 101 |
# File 'lib/sinatra/content_for.rb', line 99 def yield_content(key, *args) content_blocks[key.to_sym].map { |b| capture(*args, &b) }.join end |