protected function addGetTemplate(IfwPsn_Vendor_Twig_Compiler $compiler) { if ($this->getNode('expr') instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant) { $compiler->write("\$this->env->loadTemplate(")->subcompile($this->getNode('expr'))->raw(")"); } else { $compiler->write("\$template = \$this->env->resolveTemplate(")->subcompile($this->getNode('expr'))->raw(");\n")->write('$template'); } }
/** * Compiles the node to PHP. * * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance */ public function compile(IfwPsn_Vendor_Twig_Compiler $compiler) { if ($this->getAttribute('else')) { $compiler->write("\$context['_iterated'] = true;\n"); } if ($this->getAttribute('with_loop')) { $compiler->write("++\$context['loop']['index0'];\n")->write("++\$context['loop']['index'];\n")->write("\$context['loop']['first'] = false;\n"); if (!$this->getAttribute('ifexpr')) { $compiler->write("if (isset(\$context['loop']['length'])) {\n")->indent()->write("--\$context['loop']['revindex0'];\n")->write("--\$context['loop']['revindex'];\n")->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")->outdent()->write("}\n"); } } }
/** * Compiles the node to PHP. * * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance */ public function compile(IfwPsn_Vendor_Twig_Compiler $compiler) { $compiler->addDebugInfo($this)->write("\$context['_parent'] = (array) \$context;\n")->write("\$context['_seq'] = ifwpsn_twig_ensure_traversable(")->subcompile($this->getNode('seq'))->raw(");\n"); if (null !== $this->getNode('else')) { $compiler->write("\$context['_iterated'] = false;\n"); } if ($this->getAttribute('with_loop')) { $compiler->write("\$context['loop'] = array(\n")->write(" 'parent' => \$context['_parent'],\n")->write(" 'index0' => 0,\n")->write(" 'index' => 1,\n")->write(" 'first' => true,\n")->write(");\n"); if (!$this->getAttribute('ifexpr')) { $compiler->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")->indent()->write("\$length = count(\$context['_seq']);\n")->write("\$context['loop']['revindex0'] = \$length - 1;\n")->write("\$context['loop']['revindex'] = \$length;\n")->write("\$context['loop']['length'] = \$length;\n")->write("\$context['loop']['last'] = 1 === \$length;\n")->outdent()->write("}\n"); } } $this->loop->setAttribute('else', null !== $this->getNode('else')); $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop')); $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr')); $compiler->write("foreach (\$context['_seq'] as ")->subcompile($this->getNode('key_target'))->raw(" => ")->subcompile($this->getNode('value_target'))->raw(") {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("}\n"); if (null !== $this->getNode('else')) { $compiler->write("if (!\$context['_iterated']) {\n")->indent()->subcompile($this->getNode('else'))->outdent()->write("}\n"); } $compiler->write("\$_parent = \$context['_parent'];\n"); // remove some "private" loop variables (needed for nested loops) $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\'' . $this->getNode('key_target')->getAttribute('name') . '\'], $context[\'' . $this->getNode('value_target')->getAttribute('name') . '\'], $context[\'_parent\'], $context[\'loop\']);' . "\n"); // keep the values set in the inner context for variables defined in the outer context $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n"); }
/** * Compiles the node to PHP. * * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance */ public function compile(IfwPsn_Vendor_Twig_Compiler $compiler) { $compiler->addDebugInfo($this); if (count($this->getNode('names')) > 1) { $compiler->write('list('); foreach ($this->getNode('names') as $idx => $node) { if ($idx) { $compiler->raw(', '); } $compiler->subcompile($node); } $compiler->raw(')'); } else { if ($this->getAttribute('capture')) { $compiler->write("ob_start();\n")->subcompile($this->getNode('values')); } $compiler->subcompile($this->getNode('names'), false); if ($this->getAttribute('capture')) { $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new IfwPsn_Vendor_Twig_Markup(\$tmp, \$this->env->getCharset())"); } } if (!$this->getAttribute('capture')) { $compiler->raw(' = '); if (count($this->getNode('names')) > 1) { $compiler->write('array('); foreach ($this->getNode('values') as $idx => $value) { if ($idx) { $compiler->raw(', '); } $compiler->subcompile($value); } $compiler->raw(')'); } else { if ($this->getAttribute('safe')) { $compiler->raw("('' === \$tmp = ")->subcompile($this->getNode('values'))->raw(") ? '' : new IfwPsn_Vendor_Twig_Markup(\$tmp, \$this->env->getCharset())"); } else { $compiler->subcompile($this->getNode('values')); } } } $compiler->raw(";\n"); }
/** * Compiles the node to PHP. * * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance */ public function compile(IfwPsn_Vendor_Twig_Compiler $compiler) { $compiler->addDebugInfo($this)->write(sprintf("public function get%s(", $this->getAttribute('name'))); $count = count($this->getNode('arguments')); $pos = 0; foreach ($this->getNode('arguments') as $name => $default) { $compiler->raw('$_' . $name . ' = ')->subcompile($default); if (++$pos < $count) { $compiler->raw(', '); } } $compiler->raw(")\n")->write("{\n")->indent(); if (!count($this->getNode('arguments'))) { $compiler->write("\$context = \$this->env->getGlobals();\n\n"); } else { $compiler->write("\$context = \$this->env->mergeGlobals(array(\n")->indent(); foreach ($this->getNode('arguments') as $name => $default) { $compiler->write('')->string($name)->raw(' => $_' . $name)->raw(",\n"); } $compiler->outdent()->write("));\n\n"); } $compiler->write("\$blocks = array();\n\n")->write("ob_start();\n")->write("try {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("} catch (Exception \$e) {\n")->indent()->write("ob_end_clean();\n\n")->write("throw \$e;\n")->outdent()->write("}\n\n")->write("return ('' === \$tmp = ob_get_clean()) ? '' : new IfwPsn_Vendor_Twig_Markup(\$tmp, \$this->env->getCharset());\n")->outdent()->write("}\n\n"); }
/** * Compiles the node to PHP. * * @param IfwPsn_Vendor_Twig_Compiler A IfwPsn_Vendor_Twig_Compiler instance */ public function compile(IfwPsn_Vendor_Twig_Compiler $compiler) { $compiler->addDebugInfo($this); for ($i = 0, $count = count($this->getNode('tests')); $i < $count; $i += 2) { if ($i > 0) { $compiler->outdent()->write("} elseif ("); } else { $compiler->write('if ('); } $compiler->subcompile($this->getNode('tests')->getNode($i))->raw(") {\n")->indent()->subcompile($this->getNode('tests')->getNode($i + 1)); } if ($this->hasNode('else') && null !== $this->getNode('else')) { $compiler->outdent()->write("} else {\n")->indent()->subcompile($this->getNode('else')); } $compiler->outdent()->write("}\n"); }
protected function addGetTemplate(IfwPsn_Vendor_Twig_Compiler $compiler) { $compiler->write("\$this->env->loadTemplate(")->string($this->getAttribute('filename'))->raw(', ')->string($this->getAttribute('index'))->raw(")"); }
protected function compileDisplayBody(IfwPsn_Vendor_Twig_Compiler $compiler) { $compiler->write("\$this->checkSecurity();\n"); parent::compileDisplayBody($compiler); }
protected function compileLoadTemplate(IfwPsn_Vendor_Twig_Compiler $compiler, $node, $var) { if ($node instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant) { $compiler->write(sprintf("%s = \$this->env->loadTemplate(", $var))->subcompile($node)->raw(");\n"); } else { $compiler->write(sprintf("%s = ", $var))->subcompile($node)->raw(";\n")->write(sprintf("if (!%s", $var))->raw(" instanceof IfwPsn_Vendor_Twig_Template) {\n")->indent()->write(sprintf("%s = \$this->env->loadTemplate(%s);\n", $var, $var))->outdent()->write("}\n"); } }