Class: Liquid::IncludeFile

Inherits:
Tag
  • Object
show all
Defined in:
lib/smithy/liquid/tags/include_file.rb

Overview

IncludeFile allows templates to relate with other templates

Simply include another template:

{% include_file 'product' %}

IncludeFile a template with a local variable:

{% include_file 'product' with products[0] %}

IncludeFile a template for a collection:

{% include_file 'product' for products %}

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ IncludeFile

Returns a new instance of IncludeFile.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smithy/liquid/tags/include_file.rb', line 20

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_file' - Valid syntax: include_file '[template]' (with|for) [object|collection]")
  end

  super
end

Instance Method Details

#parse(tokens) ⇒ Object



38
39
# File 'lib/smithy/liquid/tags/include_file.rb', line 38

def parse(tokens)
end

#render(context) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/smithy/liquid/tags/include_file.rb', line 41

def render(context)
  partial = load_cached_partial(context)
  variable = context[@variable_name || @template_name[1..-2]]

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

    if variable.is_a?(Array)
      variable.collect do |var|
        context[@template_name[1..-2]] = var
        partial.render(context)
      end
    else
      context[@template_name[1..-2]] = variable
      partial.render(context)
    end
  end
end