Class: Liquid::Include

Inherits:
Tag
  • Object
show all
Defined in:
lib/liquid/tags/include.rb,
lib/liquid/profiler/hooks.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}+))?/o

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #nodelist, #tag_name

Instance Method Summary collapse

Methods inherited from Tag

#blank?, #name, parse, #raw

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, options) ⇒ Include


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/liquid/tags/include.rb', line 21

def initialize(tag_name, markup, options)
  super

  if markup =~ Syntax

    template_name = $1
    variable_name = $3

    @variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
    @template_name_expr = Expression.parse(template_name)
    @attributes = {}

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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes


19
20
21
# File 'lib/liquid/tags/include.rb', line 19

def attributes
  @attributes
end

#template_name_exprObject (readonly)

Returns the value of attribute template_name_expr


19
20
21
# File 'lib/liquid/tags/include.rb', line 19

def template_name_expr
  @template_name_expr
end

#variable_name_exprObject (readonly)

Returns the value of attribute variable_name_expr


19
20
21
# File 'lib/liquid/tags/include.rb', line 19

def variable_name_expr
  @variable_name_expr
end

Instance Method Details

#parse(_tokens) ⇒ Object


42
43
# File 'lib/liquid/tags/include.rb', line 42

def parse(_tokens)
end

#render_with_profiling(context) ⇒ Object Also known as: render


14
15
16
17
18
# File 'lib/liquid/profiler/hooks.rb', line 14

def render_with_profiling(context)
  Profiler.profile_children(context.evaluate(@template_name_expr).to_s) do
    render_without_profiling(context)
  end
end