Method: Sinatra::ContentFor#content_for
- Defined in:
- lib/sinatra/content_for.rb
#content_for(key, value = nil, options = {}, &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 also pass an immediate value instead of a block:
<% content_for :title, "foo" %>
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
132 133 134 135 136 |
# File 'lib/sinatra/content_for.rb', line 132 def content_for(key, value = nil, = {}, &block) block ||= proc { |*| value } clear_content_for(key) if [:flush] content_blocks[key.to_sym] << capture_later(&block) end |