Class: Liquid::Include

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

Overview

Include allows templates to relate with other templates

Simply include another template:

{% include 'product' %}

Include a template with a local variable:

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

Include a template for a collection:

{% include 'product' for products %}

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

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

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #nodelist, #tag_name

Instance Method Summary collapse

Methods inherited from Tag

#blank?, disable_tags, #name, parse, #raw, #render

Methods included from ParserSwitching

#parse_with_selected_parser, #strict_parse_with_error_mode_fallback

Constructor Details

#initialize(tag_name, markup, options) ⇒ Include

Returns a new instance of Include.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/liquid/tags/include.rb', line 26

def initialize(tag_name, markup, options)
  super

  if markup =~ SYNTAX

    template_name = Regexp.last_match(1)
    variable_name = Regexp.last_match(3)

    @alias_name         = Regexp.last_match(5)
    @variable_name_expr = variable_name ? parse_expression(variable_name) : nil
    @template_name_expr = parse_expression(template_name)
    @attributes         = {}

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

  else
    raise SyntaxError, options[:locale].t("errors.syntax.include")
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


24
25
26
# File 'lib/liquid/tags/include.rb', line 24

def attributes
  @attributes
end

#template_name_exprObject (readonly)

Returns the value of attribute template_name_expr


24
25
26
# File 'lib/liquid/tags/include.rb', line 24

def template_name_expr
  @template_name_expr
end

#variable_name_exprObject (readonly)

Returns the value of attribute variable_name_expr


24
25
26
# File 'lib/liquid/tags/include.rb', line 24

def variable_name_expr
  @variable_name_expr
end

Instance Method Details

#parse(_tokens) ⇒ Object


48
49
# File 'lib/liquid/tags/include.rb', line 48

def parse(_tokens)
end

#render_to_output_buffer(context, output) ⇒ Object

Raises:


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/liquid/tags/include.rb', line 51

def render_to_output_buffer(context, output)
  template_name = context.evaluate(@template_name_expr)
  raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name

  partial = PartialCache.load(
    template_name,
    context: context,
    parse_context: parse_context
  )

  context_variable_name = @alias_name || template_name.split('/').last

  variable = if @variable_name_expr
    context.evaluate(@variable_name_expr)
  else
    context.find_variable(template_name, raise_on_not_found: false)
  end

  old_template_name = context.template_name
  old_partial       = context.partial
  begin
    context.template_name = template_name
    context.partial       = true
    context.stack do
      @attributes.each do |key, value|
        context[key] = context.evaluate(value)
      end

      if variable.is_a?(Array)
        variable.each do |var|
          context[context_variable_name] = var
          partial.render_to_output_buffer(context, output)
        end
      else
        context[context_variable_name] = variable
        partial.render_to_output_buffer(context, output)
      end
    end
  ensure
    context.template_name = old_template_name
    context.partial       = old_partial
  end

  output
end