public function compile(Compiler $compiler, TagNode $node) { if ($node->hasChild('else')) { //extract data to be traversed $compiler->indented('$temp = ')->compileNode($node->getChild('source'))->add(';'); //generate check and code for the 'else' branch $compiler->indented('if(empty($temp)) {')->indent()->compileNode($node->getChild('else'))->outdent()->indented('} else {')->indent(); $source = new TempVariableNode('temp'); } else { $source = $node->getChild('source'); } $compiler->indented('foreach(')->compileNode($source)->add(' as '); if ($node->hasChild('loop_key')) { $compiler->compileNode($node->getChild('loop_key'))->add(' => '); } $variables = $node->getData('variables'); if ($variables === 1) { $compiler->compileNode($node->getChild('loop_variable_0'))->add(') {')->indent(); } else { $arguments = []; for ($i = 0; $i < $variables; ++$i) { $arguments[] = $node->getChild('loop_variable_' . $i); } $compiler->add('$loopVariable) {')->indent()->indented('list')->compileArgumentList($arguments)->add(' = $loopVariable;'); } $compiler->compileNode($node->getChild('loop_body'))->outdent()->indented('}'); if ($node->hasChild('else')) { //bracket opened after if-empty check $compiler->outdent()->indented('}'); } }