Module: Sinatra::ContentFor
- Defined in:
- lib/mongodb_logger/server/helpers/content_for.rb
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.
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
16 17 18 |
# File 'lib/mongodb_logger/server/helpers/content_for.rb', line 16 def content_for(key, &block) content_blocks[key.to_sym] << 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
.
NOTICE that you call this without an =
sign. IE, in a <% %>
block, and not in a <%= %>
block.
40 41 42 43 44 45 46 47 48 |
# File 'lib/mongodb_logger/server/helpers/content_for.rb', line 40 def yield_content(key, *args) content_blocks[key.to_sym].map do |content| if respond_to?(:block_is_haml?) && block_is_haml?(content) capture_haml(*args, &content) else content.call(*args) end end.join end |