Module: Sinatra::ContentFor

Defined in:
lib/sinatra/content_for.rb

Instance Method Summary collapse

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.



13
14
15
16
17
18
19
20
21
# File 'lib/sinatra/content_for.rb', line 13

def content_for(key, &block)
  content_blocks[key.to_sym] << begin
    if respond_to?(:block_is_haml?) && block_is_haml?(block)
      capture_haml(&block)
    else
      block.call
    end
  end
end

#yield_content(key) ⇒ 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.



32
33
34
# File 'lib/sinatra/content_for.rb', line 32

def yield_content(key)
  content_blocks[key.to_sym].join
end