Class: Twig::Node::Yield

Inherits:
Base
  • Object
show all
Defined in:
lib/twig/node/yield.rb

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, body, arguments, lineno) ⇒ Yield

Returns a new instance of Yield.



6
7
8
9
# File 'lib/twig/node/yield.rb', line 6

def initialize(expr, body, arguments, lineno)
  arguments = arguments.empty? ? {} : { arguments: }
  super({ expr:, body: }, arguments, lineno)
end

Instance Method Details

#compile(compiler) ⇒ Object



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twig/node/yield.rb', line 11

def compile(compiler)
  compiler.
    add_debug_info(self).
    write('context.output_buffer.append = (').
    subcompile(nodes[:expr]).
    raw(' do')

  if attributes.key?(:arguments)
    compiler.
      raw(" |#{attributes[:arguments].join(', ')}|")
  end

  compiler.
    raw("\n").
    indent.
    write("context.buffer_and_return do\n").
    indent

  if attributes.key?(:arguments)
    compiler.
      write("context.push_stack\n").
      write('context.merge!({')

    attributes[:arguments].each do |argument|
      compiler.raw("#{argument}:,")
    end

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

  compiler.
    subcompile(nodes[:body]).
    raw("\n")

  if attributes.key?(:arguments)
    compiler.
      write("context.pop_stack\n")
  end

  compiler.
    outdent.
    write("end.to_s.html_safe\n").
    outdent.
    write("end);\n")
end