Class: Twig::Node::For
Instance Attribute Summary
Attributes inherited from Base
#attributes, #lineno, #nodes, #source_context, #tag
Instance Method Summary collapse
- #compile(compiler) ⇒ Object
-
#initialize(key_target, value_target, seq, if_expr, body, else_expr, lineno) ⇒ For
constructor
A new instance of For.
Methods inherited from Base
#empty?, #length, #template_name, #to_s
Constructor Details
#initialize(key_target, value_target, seq, if_expr, body, else_expr, lineno) ⇒ For
Returns a new instance of For.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/twig/node/for.rb', line 6 def initialize(key_target, value_target, seq, if_expr, body, else_expr, lineno) unless if_expr.nil? body = If.new(Nodes.new(AutoHash.new.add(if_expr, body)), nil, lineno) end loop = ForLoop.new(lineno) body = Nodes.new(AutoHash.new.add(body, loop)) nodes = { key_target:, value_target:, seq:, body:, } unless else_expr.nil? nodes[:else_expr] = else_expr end super(nodes, { with_loop: true }, lineno) end |
Instance Method Details
#compile(compiler) ⇒ Object
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/twig/node/for.rb', line 28 def compile(compiler) iteration_var = compiler.var_name function_var = compiler.var_name compiler. add_debug_info(self). write("#{iteration_var} = ::Twig::Runtime::LoopIterator.new("). subcompile(nodes[:seq]). raw(")\n"). write("#{function_var} = lambda do |iterator, context, blocks, recurse, depth|\n"). indent. write("parent = context.dup\n") if attributes[:with_loop] compiler. write('context[:loop] = ::Twig::Runtime::LoopContext.new('). raw("iterator, parent, blocks, recurse, depth)\n") end if nodes.key?(:else_expr) compiler.write("context[:_iterated] = false\n") end key_var = compiler.var_name value_var = compiler.var_name compiler. write("iterator.each do |#{key_var}, #{value_var}|\n"). indent. write(''). subcompile(nodes[:key_target]). raw(" = #{key_var}\n"). write(''). subcompile(nodes[:value_target]). raw(" = #{value_var}\n"). subcompile(nodes[:body]). outdent. write("end\n") if nodes.key?(:else_expr) compiler. write("unless context[:_iterated]\n"). indent. subcompile(nodes[:else_expr]). outdent. write("end\n") end compiler. write("context.remove!(:#{nodes[:key_target].attributes[:name]}, "). raw(":#{nodes[:value_target].attributes[:name]}") if attributes[:with_loop] compiler.raw(', :loop') end compiler. raw(")\n"). write("context.keep!(parent.keys)\n"). write("context.merge!(parent, overwrite: false)\n"). outdent. write("end\n"). write("#{function_var}.call(#{iteration_var}, context, blocks, #{function_var}, 0)\n") end |