Exemplo n.º 1
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("\$context['_parent'] = \$context;\n")->write("\$context['_seq'] = 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");
 }
Exemplo n.º 2
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     if ($this->getAttribute('output')) {
         $compiler->addDebugInfo($this)->write('$this->displayParentBlock(')->string($this->getAttribute('name'))->raw(", \$context, \$blocks);\n");
     } else {
         $compiler->raw('$this->renderParentBlock(')->string($this->getAttribute('name'))->raw(', $context, $blocks)');
     }
 }
Exemplo n.º 3
0
 /**
  * Compile
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $compiler->addDebugInfo($this);
     if ($this->isSpecial()) {
         $compiler->raw($this->specialVars[$name]);
     } elseif ($this->getAttribute('always_defined')) {
         $compiler->raw('$context[')->string($name)->raw(']');
     } else {
         $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : null)');
     }
 }
Exemplo n.º 4
0
 public function compile(Apishka_Templater_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");
 }
Exemplo n.º 5
0
 /**
  * Compile
  *
  * @param Apishka_Templater_Compiler $compiler
  */
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     if ($this->getAttribute('as_string')) {
         $compiler->raw('(string) ');
     }
     $args = $this->getNode('args');
     if ($this->getAttribute('output')) {
         $compiler->addDebugInfo($this)->write('$this->displayBlock(')->subcompile($this->getNode('name'))->raw(', ');
         $this->compileContext($compiler, $args);
         $compiler->write('$blocks);' . PHP_EOL);
     } else {
         $compiler->raw('$this->renderBlock(')->subcompile($this->getNode('name'))->raw(', ');
         $this->compileContext($compiler, $args);
         $compiler->write('$blocks)');
     }
 }
Exemplo n.º 6
0
 public function compile(Apishka_Templater_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 Apishka_Templater_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 Apishka_Templater_Markup(\$tmp, \$this->env->getCharset())");
             } else {
                 $compiler->subcompile($this->getNode('values'));
             }
         }
     }
     $compiler->raw(";\n");
 }
Exemplo n.º 7
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write(sprintf("public function block_%s(\$context, array \$blocks = array())\n", $this->getAttribute('name')), "{\n")->indent();
     $compiler->subcompile($this->getNode('body'))->outdent()->write("}\n\n");
 }
Exemplo n.º 8
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('')->subcompile($this->getNode('expr'))->raw(";\n");
 }
Exemplo n.º 9
0
 protected function compileDisplay(Apishka_Templater_Compiler $compiler)
 {
     $compiler->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n")->indent()->subcompile($this->getNode('display_start'))->subcompile($this->getNode('body'));
     if (null !== ($parent = $this->getNode('parent'))) {
         $compiler->addDebugInfo($parent);
         if ($parent instanceof Apishka_Templater_Node_Expression_Constant) {
             $compiler->write('$this->parent');
         } else {
             $compiler->write('$this->getParent($context)');
         }
         $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
     }
     $compiler->subcompile($this->getNode('display_end'))->outdent()->write("}\n\n");
 }
Exemplo n.º 10
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('echo ')->string($this->getAttribute('data'))->raw(";\n");
 }
Exemplo n.º 11
0
 public function compile(Apishka_Templater_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write(sprintf("\$this->displayBlock('%s', \$context, \$blocks);\n", $this->getAttribute('name')));
 }