Class: Liquid::Include

Inherits:
Tag
  • Object
show all
Defined in:
app/liquid/tags/cms_include_tag.rb,
lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/tags/include.rb

Constant Summary collapse

Syntax =
/(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Tag

#name

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Include

Returns a new instance of Include.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/tags/include.rb', line 5

def initialize(tag_name, markup, tokens)      
  if markup =~ Syntax

    @template_name = $1        
    @variable_name = $3
    @attributes    = {}

    markup.scan(TagAttributes) do |key, value|
      @attributes[key] = value
    end

  else
    raise SyntaxError.new("Error in tag 'include' - Valid syntax: include '[template]' (with|for) [object|collection]")
  end

  super
end

Instance Method Details

#parse(tokens) ⇒ Object



23
24
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/lib/liquid/tags/include.rb', line 23

def parse(tokens)      
end

#render(context) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/liquid/tags/cms_include_tag.rb', line 6

def render(context)
  template_name = context[@template_name]
  raise FileSystemError, "Illegal page template name '#{template_name}'" unless template_name =~ Cms::Page::NAME_REGEX

  page = context.registers[:context].pages.find_by_name(template_name)
  raise FileSystemError, "No such page template '#{template_name}'" if page.nil?
  
  source  = page.content
  partial = Liquid::Template.parse(source)

  variable = context[@variable_name || template_name]

  context.stack do
    @attributes.each do |key, value|
      context[key] = context[value]
    end

    if variable.is_a?(Array)
      
      variable.collect do |variable|            
        context[template_name] = variable
        partial.render(context)
      end

    else
                
      context[template_name] = variable
      partial.render(context)
      
    end
  end
end