Class: Twig::Node::With
Instance Attribute Summary
Attributes inherited from Base
#attributes, #lineno, #nodes, #source_context, #tag
Instance Method Summary collapse
- #compile(compiler) ⇒ Object
-
#initialize(body, variables, only, lineno) ⇒ With
constructor
A new instance of With.
Methods inherited from Base
#empty?, #length, #template_name, #to_s
Constructor Details
#initialize(body, variables, only, lineno) ⇒ With
Returns a new instance of With.
10 11 12 13 14 15 |
# File 'lib/twig/node/with.rb', line 10 def initialize(body, variables, only, lineno) nodes = { body: } nodes[:variables] = variables unless variables.nil? super(nodes, { only: }, lineno) end |
Instance Method Details
#compile(compiler) ⇒ Object
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 |
# File 'lib/twig/node/with.rb', line 17 def compile(compiler) compiler. add_debug_info(self). write("context.push_stack\n") if nodes.key?(:variables) if attributes[:only] compiler.write("context.clear\n") end var_name = compiler.var_name compiler. write("#{var_name} = "). subcompile(nodes[:variables]). write("\n"). write("unless #{var_name}.is_a?(::Hash) || #{var_name} == []\n"). indent. write("raise ::Twig::Error::Syntax.new('Variables passed to the \"with\" tag must be a mapping.',"). repr(nodes[:variables].lineno). raw(", source_context)\n"). outdent. write("end\n"). write("context.merge!(env.globals)\n"). write("context.merge!(#{var_name})\n") end compiler. subcompile(nodes[:body]). write("context.pop_stack\n") end |