Class: Roger::TemplateContext
- Inherits:
-
Object
- Object
- Roger::TemplateContext
- Defined in:
- lib/roger/template.rb
Overview
The context that is passed to all templates
Instance Attribute Summary collapse
-
#_content_for_blocks ⇒ Object
Returns the value of attribute _content_for_blocks.
Instance Method Summary collapse
-
#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.
-
#document ⇒ Object
Access to the front-matter of the document (if any).
-
#env ⇒ Object
The current environment variables.
-
#initialize(template, env = {}) ⇒ TemplateContext
constructor
A new instance of TemplateContext.
- #partial(name, options = {}, &block) ⇒ Object
-
#template ⇒ Object
The current Roger::Template in use.
Constructor Details
#initialize(template, env = {}) ⇒ TemplateContext
Returns a new instance of TemplateContext.
168 169 170 171 172 173 174 175 |
# File 'lib/roger/template.rb', line 168 def initialize(template, env = {}) @_content_for_blocks = {} @_template = template @_env = env # Block counter to make sure erbtemp binding is always unique @block_counter = 0 end |
Instance Attribute Details
#_content_for_blocks ⇒ Object
Returns the value of attribute _content_for_blocks.
166 167 168 |
# File 'lib/roger/template.rb', line 166 def _content_for_blocks @_content_for_blocks end |
Instance Method Details
#capture(&block) ⇒ Object
rubocop:disable Lint/Eval
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/roger/template.rb', line 209 def capture(&block) unless template.template.is_a?(Tilt::ERBTemplate) fail ArgumentError, "content_for works only with ERB Templates" end @block_counter += 1 counter = @block_counter eval "@_erbout_tmp#{counter} = _erbout", block.binding eval "_erbout = \"\"", block.binding t = Tilt::ERBTemplate.new { "<%= 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 %>
“‘
204 205 206 |
# File 'lib/roger/template.rb', line 204 def content_for(block_name, &block) @_content_for_blocks[block_name] = capture(&block) end |
#document ⇒ Object
Access to the front-matter of the document (if any)
183 184 185 |
# File 'lib/roger/template.rb', line 183 def document @_data ||= OpenStruct.new(template.data) end |
#env ⇒ Object
The current environment variables.
188 189 190 |
# File 'lib/roger/template.rb', line 188 def env @_env end |
#partial(name, options = {}, &block) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/roger/template.rb', line 225 def partial(name, = {}, &block) template_path = template.find_template(name, :partials_path) if template_path out = render_partial(template_path, , &block) if block_given? eval "_erbout.concat(#{out.dump})", block.binding else out end else fail ArgumentError, "No such partial #{name}, referenced from #{template.source_path}" end end |
#template ⇒ Object
The current Roger::Template in use
178 179 180 |
# File 'lib/roger/template.rb', line 178 def template @_template end |