Class: HtmlMockup::TemplateContext
- Inherits:
-
Object
- Object
- HtmlMockup::TemplateContext
- Defined in:
- lib/html_mockup/template.rb
Instance Attribute Summary collapse
-
#_content_for_blocks ⇒ Object
Returns the value of attribute _content_for_blocks.
Instance Method Summary collapse
-
#content_for(block_name, &capture) ⇒ 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 = {}) ⇒ Object
-
#template ⇒ Object
The current HtmlMockup::Template in use.
Constructor Details
#initialize(template, env = {}) ⇒ TemplateContext
Returns a new instance of TemplateContext.
151 152 153 |
# File 'lib/html_mockup/template.rb', line 151 def initialize(template, env={}) @_template, @_env = template, @_content_for_blocks = {}, env end |
Instance Attribute Details
#_content_for_blocks ⇒ Object
Returns the value of attribute _content_for_blocks.
149 150 151 |
# File 'lib/html_mockup/template.rb', line 149 def _content_for_blocks @_content_for_blocks end |
Instance Method Details
#content_for(block_name, &capture) ⇒ 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 %>
“‘
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/html_mockup/template.rb', line 182 def content_for(block_name, &capture) raise ArgumentError, "content_for works only with ERB Templates" if !self.template.template.kind_of?(Tilt::ERBTemplate) eval "@_erbout_tmp = _erbout", capture.binding eval "_erbout = \"\"", capture.binding t = Tilt::ERBTemplate.new(){ "<%= yield %>" } @_content_for_blocks[block_name] = t.render(&capture) return nil ensure eval "_erbout = @_erbout_tmp", capture.binding end |
#document ⇒ Object
Access to the front-matter of the document (if any)
161 162 163 |
# File 'lib/html_mockup/template.rb', line 161 def document @_data ||= OpenStruct.new(self.template.data) end |
#env ⇒ Object
The current environment variables.
166 167 168 |
# File 'lib/html_mockup/template.rb', line 166 def env @_env end |
#partial(name, options = {}) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/html_mockup/template.rb', line 193 def partial(name, = {}) if template_path = self.template.find_template(name, :partials_path) partial_template = Tilt.new(template_path.to_s) partial_template.render(self, [:locals] || {}) elsif template_path = self.template.find_template(name + ".part", :partials_path) template = Tilt::ERBTemplate.new(template_path.to_s) context = MockupTemplate::TemplateContext.new([:locals] || {}) template.render(context, :env => self.env) else raise ArgumentError, "No such partial #{name}, referenced from #{self.template.source_path}" end end |
#template ⇒ Object
The current HtmlMockup::Template in use
156 157 158 |
# File 'lib/html_mockup/template.rb', line 156 def template @_template end |