class Armadillo::TemplateContext
@api private
Public Class Methods
Initialize the TemplateContext
.
@param source [Object] @param template_options [Hash] ({})
# File lib/armadillo.rb, line 21 def initialize(source, template_options = {}) @template_options = template_options super(source) end
Public Instance Methods
Create a new frame with previous frame as parent.
# File lib/armadillo.rb, line 64 def create_frame @current_frame = { :vlocks => {}, :parent_frame => current_frame } end
Extend the specified template in which the inner view blocks will be rendered.
@note
This is a template instruction.
@param template_path [String] @param locals [Hash]
# File lib/armadillo.rb, line 34 def extends(template_path, locals = {}) @extends_data = [template_path, locals] end
Determine if the current template should extend from a new template.
@return [Boolean]
# File lib/armadillo.rb, line 84 def extends? !! @extends_data end
Return and delete the extract data.
@return [Array<(String, Hash)>]
The extended template name and the locals.
# File lib/armadillo.rb, line 92 def extract_extends_data @extends_data.tap { @extends_data = nil } end
Render another template with the same options as the current one.
@param template_path [String] @param locals [Hash] ({})
@return [String]
# File lib/armadillo.rb, line 77 def render(template_path, locals = {}) Armadillo.render(template_path, locals, @template_options) end
Determine the contents or specify the place in which the view block will be rendered.
@note
This is a template instruction.
@param block_name [Symbol] @param block [Block]
# File lib/armadillo.rb, line 46 def vlock(block_name, &block) if current_frame && extends? # Save child view block to be rendered on the parent raise "No block given on `#{block_name}`" unless block current_frame[:vlocks][block_name] = block elsif current_frame && (frame = get_frame(block_name, current_frame)) # Look for child view block and render it temporary_frame(frame[:parent_frame]) do frame[:vlocks][block_name].call end elsif block # Render a default block block.call end end
Private Instance Methods
Get the current frame. Each frame contains the blocks specified using vlock
and its parent frame.
@return [Hash]
# File lib/armadillo.rb, line 102 def current_frame @current_frame end
Get the block from the frames stack by its name.
@param block_name [Symbol]
# File lib/armadillo.rb, line 120 def get_frame(block_name, frame) if frame[:vlocks].has_key?(block_name) frame elsif frame[:parent_frame] get_frame(block_name, frame[:parent_frame]) end end
Create a temporary current frame for the block to be executed.
@param frame [Hash] @param block [Block]
# File lib/armadillo.rb, line 110 def temporary_frame(frame, &block) old = current_frame @current_frame = frame block.call @current_frame = old end