Class: Twig::Node::Include

Inherits:
Base
  • Object
show all
Includes:
Output
Defined in:
lib/twig/node/include.rb

Direct Known Subclasses

Embed

Instance Attribute Summary

Attributes inherited from Base

#attributes, #lineno, #nodes, #source_context, #tag

Instance Method Summary collapse

Methods inherited from Base

#empty?, #length, #template_name, #to_s

Constructor Details

#initialize(expr, variables, only, ignore_missing, lineno) ⇒ Include

Returns a new instance of Include.

Parameters:



13
14
15
16
17
18
19
20
21
# File 'lib/twig/node/include.rb', line 13

def initialize(expr, variables, only, ignore_missing, lineno)
  nodes = { expr: }
  nodes[:variables] = variables if variables

  super(nodes, {
    only:,
    ignore_missing:,
  }, lineno)
end

Instance Method Details

#compile(compiler) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/twig/node/include.rb', line 23

def compile(compiler)
  compiler.add_debug_info(self)

  if attributes[:ignore_missing]
    template = compiler.var_name

    compiler.
      write("begin\n").
      indent.
      write("#{template} = ")

    add_get_template(compiler, template)

    compiler.
      raw("\n").
      outdent.
      write("rescue ::Twig::Error::Loader => e\n").
      indent.
      write("# ignore missing template\n").
      write("#{template} = nil\n").
      outdent.
      write("end\n").
      write("if #{template}\n").
      indent.
      write("#{template}.render_with_blocks(")

    add_template_arguments(compiler)

    compiler.
      raw(");\n").
      outdent.
      write("end\n")
  else
    compiler.
      write('')

    add_get_template(compiler)

    compiler.
      raw('.render(')

    add_template_arguments(compiler)

    compiler.
      raw(");\n")
  end
end