Class: Liquid2::IncludeTag

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

Overview

The standard include tag.

Instance Attribute Summary

Attributes inherited from Node

#blank, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

#name

Methods inherited from Node

#block_scope, #render_with_disabled_tag_check, #template_scope

Constructor Details

#initialize(token, name, repeat, var, as, args) ⇒ IncludeTag

Returns a new instance of IncludeTag.

Parameters:



48
49
50
51
52
53
54
55
56
# File 'lib/liquid2/nodes/tags/include.rb', line 48

def initialize(token, name, repeat, var, as, args)
  super(token)
  @template_name = name
  @repeat = repeat
  @var = var
  @as = as
  @args = args
  @blank = false
end

Class Method Details

.parse(token, parser) ⇒ IncludeTag

Parameters:

Returns:



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
38
39
40
41
# File 'lib/liquid2/nodes/tags/include.rb', line 10

def self.parse(token, parser)
  name = parser.parse_primary

  repeat = false
  var = nil # : Expression?
  as = nil # : Identifier?

  if parser.current_kind == :token_for && !%i[token_comma
                                              token_colon].include?(parser.peek_kind)
    parser.next
    repeat = true
    var = parser.parse_primary
    if parser.current_kind == :token_as
      parser.next
      as = parser.parse_identifier
    end
  elsif parser.current_kind == :token_with && !%i[token_comma
                                                  token_colon].include?(parser.peek_kind)
    parser.next
    var = parser.parse_primary
    if parser.current_kind == :token_as
      parser.next
      as = parser.parse_identifier
    end
  end

  parser.next if parser.current_kind == :token_comma
  args = parser.parse_keyword_arguments
  parser.carry_whitespace_control
  parser.eat(:token_tag_end)
  new(token, name, repeat, var, as, args)
end

Instance Method Details

#children(static_context, include_partials: true) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/liquid2/nodes/tags/include.rb', line 90

def children(static_context, include_partials: true)
  return [] unless include_partials

  name = static_context.evaluate(@template_name)
  template = static_context.env.get_template(name.to_s, context: static_context, tag: :include)
  template.ast
rescue LiquidTemplateNotFoundError => e
  e.token = @token
  e.template_name = static_context.template.full_name
  raise e
end

#expressionsObject



102
103
104
105
106
107
# File 'lib/liquid2/nodes/tags/include.rb', line 102

def expressions
  exprs = [@template_name]
  exprs << @var if @var
  exprs.concat(@args.map(&:value))
  exprs
end

#partial_scopeObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/liquid2/nodes/tags/include.rb', line 109

def partial_scope
  scope = @args.map { |arg| Identifier.new([:token_word, arg.name, arg.token.last]) }

  if @var
    if @as
      scope << @as # steep:ignore
    elsif @template_name.is_a?(String)
      scope << Identifier.new([:token_word, @template_name.split(".").first, @token.last])
    end
  end

  Partial.new(@template_name, :shared, scope)
end

#render(context, buffer) ⇒ Object



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
# File 'lib/liquid2/nodes/tags/include.rb', line 58

def render(context, buffer)
  name = context.evaluate(@template_name)
  template = context.env.get_template(name.to_s, context: context, tag: :include)
  namespace = @args.to_h { |arg| [arg.name, context.evaluate(arg.value)] }

  context.extend(namespace, template: template) do
    if @var
      val = context.evaluate(@var || raise)
      key = @as&.name || template.name.split(".").first

      if val.is_a?(Array)
        context.raise_for_loop_limit(length: val.size)
        index = 0
        while index < val.length
          namespace[key] = val[index]
          template.render_with_context(context, buffer, partial: true, block_scope: false)
          index += 1
        end
      else
        namespace[key] = val
        template.render_with_context(context, buffer, partial: true, block_scope: false)
      end
    else
      template.render_with_context(context, buffer, partial: true, block_scope: false)
    end
  end
rescue LiquidTemplateNotFoundError => e
  e.token = @token
  e.template_name = context.template.full_name unless context.template.full_name.empty?
  raise e
end