Class: HtmlEmailCreator::IncludeTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/html_email_creator/tags/include_tag.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ IncludeTag

Returns a new instance of IncludeTag.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/html_email_creator/tags/include_tag.rb', line 8

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

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

    markup.scan(Liquid::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



26
27
# File 'lib/html_email_creator/tags/include_tag.rb', line 26

def parse(tokens)
end

#render(context) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/html_email_creator/tags/include_tag.rb', line 29

def render(context)
  settings = find_settings(context)
  source = read_template_from_file_system(context, settings)
  partial = Liquid::Template.parse(source)
  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 |variable|
        context[@template_name[1..-2]] = variable
        partial.render(context)
      end
    else
      context[@template_name[1..-2]] = variable
      partial.render(context)
    end
  end
end