Class: HtmlMockup::TemplateContext

Inherits:
Object
  • Object
show all
Defined in:
lib/html_mockup/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_blocksObject

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

#documentObject

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

#envObject

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, options = {})
  if template_path = self.template.find_template(name, :partials_path)
    partial_template = Tilt.new(template_path.to_s)
    partial_template.render(self, options[:locals] || {})
  elsif template_path = self.template.find_template(name + ".part", :partials_path)
    template = Tilt::ERBTemplate.new(template_path.to_s)
    context = MockupTemplate::TemplateContext.new(options[:locals] || {})
    template.render(context, :env => self.env)        
  else
    raise ArgumentError, "No such partial #{name}, referenced from #{self.template.source_path}"
  end
end

#templateObject

The current HtmlMockup::Template in use



156
157
158
# File 'lib/html_mockup/template.rb', line 156

def template
  @_template
end