Module: Roger::Template::Helpers::Capture
- Included in:
- TemplateContext
- Defined in:
- lib/roger/template/helpers/capture.rb
Overview
The capture helper
Class Method Summary collapse
Instance Method Summary collapse
-
#_content_for_blocks ⇒ Object
rubocop:enable Lint/Eval.
-
#capture(&block) ⇒ Object
rubocop:disable Lint/Eval.
-
#content_for(block_name, &block) ⇒ Object
Capture content in blocks in the template for later use in the layout.
-
#content_for?(block_name) ⇒ Boolean
Check if a block will yield content.
Class Method Details
.included(base) ⇒ Object
6 7 8 9 |
# File 'lib/roger/template/helpers/capture.rb', line 6 def self.included(base) # Just the writer; the reader is below. base.send(:attr_writer, :_content_for_blocks) end |
Instance Method Details
#_content_for_blocks ⇒ Object
rubocop:enable Lint/Eval
58 59 60 |
# File 'lib/roger/template/helpers/capture.rb', line 58 def _content_for_blocks @_content_for_blocks || {} end |
#capture(&block) ⇒ Object
rubocop:disable Lint/Eval
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/roger/template/helpers/capture.rb', line 38 def capture(&block) unless Thread.current[:tilt_current_template].is_a?(Tilt::ERBTemplate) err = "content_for works only with ERB Templates" err += "(was: #{template.current_tilt_template.inspect})" raise ArgumentError, err end @block_counter ||= 0 @block_counter += 1 counter = @block_counter eval "@_erbout_tmp#{counter} = _erbout", block.binding eval "_erbout = \"\"", block.binding t = Tilt::ERBTemplate.new { "<% return yield %>" } t.render(&block) ensure eval "_erbout = @_erbout_tmp#{counter}", block.binding end |
#content_for(block_name, &block) ⇒ Object
Capture content in blocks in the template for later use in the layout. Currently only works in ERB templates. Use like this in the template:
“‘
<% content_for :name %> bla bla <% end %>
“‘
Place it like this in the layout:
“‘
<%= yield :name %>
“‘
23 24 25 26 |
# File 'lib/roger/template/helpers/capture.rb', line 23 def content_for(block_name, &block) @_content_for_blocks ||= {} @_content_for_blocks[block_name] = capture(&block) end |
#content_for?(block_name) ⇒ Boolean
Check if a block will yield content
“‘
<% if content_for? :name %> bla bla <% end %>
“‘
33 34 35 |
# File 'lib/roger/template/helpers/capture.rb', line 33 def content_for?(block_name) (!_content_for_blocks[block_name].nil? && !_content_for_blocks[block_name].empty?) end |