Beispiel #1
0
 protected function compileDisplayBody(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     $compiler->enterScope()->subcompile($node->getNode('body'))->leaveScope();
     if (null !== $node->getNode('parent')) {
         $compiler->write("this.getParent(context).render_(sb, context, twig.extend({}, this.getBlocks(), blocks));\n");
     }
 }
Beispiel #2
0
 public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     if (!$node instanceof \Twig_Node_For) {
         throw new \RuntimeException(sprintf('$node must be an instanceof of \\For, but got "%s".', get_class($node)));
     }
     $count = $this->count++;
     $suffix = $count > 0 ? $count : '';
     $seqName = 'seq' . $suffix;
     $keyName = 'k' . $suffix;
     $valueName = 'v' . $suffix;
     $iteratedName = 'iterated' . $suffix;
     $loopName = 'loop' . $suffix;
     if ($count > 0) {
         $compiler->enterScope();
     }
     $compiler->setVar('_seq', $seqName)->setVar('_iterated', $iteratedName)->setVar('loop', $loopName)->addDebugInfo($node);
     // keep parent reference as some might rely on this
     if (0 === $count) {
         $compiler->write("context['_parent'] = context;\n");
     }
     $compiler->write("var {$seqName} = ")->subcompile($node->getNode('seq'))->raw(";\n");
     if (null !== $node->getNode('else')) {
         $compiler->write("var {$iteratedName} = false;\n");
     }
     if ($node->getAttribute('with_loop')) {
         $compiler->write("var {$loopName} = {\n")->indent();
         if ($count > 0) {
             $parentSuffix = $count - 1 > 0 ? $count - 1 : '';
             $compiler->write("'parent': {loop: loop{$parentSuffix}},\n");
         }
         $compiler->write("'index0': 0,\n")->write("'index': 1,\n")->write("'first': true\n")->outdent()->write("};\n");
         if (false === $node->getAttribute('ifexpr')) {
             $compiler->write("if (twig.countable({$seqName})) {\n")->indent()->write("var length = twig.count({$seqName});\n")->write("{$loopName}['revindex0'] = length - 1;\n")->write("{$loopName}['revindex'] = length;\n")->write("{$loopName}['length'] = length;\n")->write("{$loopName}['last'] = 1 === length;\n")->outdent()->write("}\n");
         }
     }
     $ref = new \ReflectionProperty($node, 'loop');
     $ref->setAccessible(true);
     $loop = $ref->getValue($node);
     $loop->setAttribute('else', null !== $node->getNode('else'));
     $loop->setAttribute('with_loop', $node->getAttribute('with_loop'));
     $loop->setAttribute('ifexpr', $node->getAttribute('ifexpr'));
     $compiler->write("twig.forEach({$seqName}, function({$valueName}, {$keyName}) {\n")->indent()->write('')->subcompile($node->getNode('key_target'))->raw(" = {$keyName};\n")->write('')->subcompile($node->getNode('value_target'))->raw(" = {$valueName};\n")->subcompile($node->getNode('body'))->outdent()->write("}, this);\n");
     if (null !== $node->getNode('else')) {
         $compiler->write("if (!{$iteratedName}) {\n")->indent()->subcompile($node->getNode('else'))->outdent()->write("}\n");
     }
     if ($count > 0) {
         $compiler->leaveScope();
     }
     $this->count = $count;
 }
Beispiel #3
0
 public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     if (!$node instanceof \Twig_Node_Macro) {
         throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Macro, but got "%s".', get_class($node)));
     }
     $compiler->enterScope();
     $arguments = array();
     foreach ($node->getNode('arguments') as $name => $argument) {
         if ($argument->hasAttribute('name')) {
             $name = $argument->getAttribute('name');
         }
         $arguments[] = 'opt_' . $name;
         $compiler->setVar($name, 'opt_' . $name);
     }
     $compiler->addDebugInfo($node)->write("/**\n", " * Macro \"" . $node->getAttribute('name') . "\"\n", " *\n");
     foreach ($arguments as $arg => $var) {
         $compiler->write(" * @param {*} {$var}\n");
     }
     $compiler->write(" * @return {string}\n")->write(" */\n")->raw($compiler->templateFunctionName)->raw(".prototype.get")->raw($node->getAttribute('name'))->raw(" = function(" . implode(', ', $arguments) . ") {\n")->indent()->write("var context = twig.extend({}, this.env_.getGlobals());\n\n")->write("var sb = new twig.StringBuffer;\n")->subcompile($node->getNode('body'))->raw("\n")->write("return new twig.Markup(sb.toString());\n")->outdent()->write("};\n\n")->leaveScope();
 }