public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Block) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Block, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node)->write("/**\n", " * @param {!twig.StringBuffer} sb\n")->write(" * @param {Object.<*>} context\n")->write(" * @param {Object.<Function>} blocks\n", " */\n")->write("{$compiler->templateFunctionName}.prototype.block_")->raw($node->getAttribute('name') . ' = function(sb, context, blocks) {' . "\n")->indent()->enterScope()->raw("blocks = typeof(blocks) == \"undefined\" ? {} : blocks;" . "\n")->subcompile($node->getNode('body'))->leaveScope()->outdent()->write("};\n\n"); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Print) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Print, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node)->write('sb.append(')->subcompile($node->getNode('expr'))->raw(");\n"); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_BlockReference) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_BlockReference, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node)->write(sprintf("sb.append(this.renderBlock(%s, context, blocks));\n", json_encode($node->getAttribute('name')))); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Set) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Set, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node); if (count($node->getNode('names')) > 1) { $values = $node->getNode('values'); foreach ($node->getNode('names') as $idx => $subNode) { $compiler->subcompile($subNode)->raw(' = ')->subcompile($values->getNode($idx))->raw(";\n"); } return; } $count = $this->count++; $captureStringBuffer = 'cSb' . ($count > 0 ? $count : ''); if ($node->getAttribute('capture')) { $compiler->write("var {$captureStringBuffer} = sb;\n")->write("sb = new twig.StringBuffer;")->subcompile($node->getNode('values')); } $compiler->subcompile($node->getNode('names'), false); if ($node->getAttribute('capture')) { $compiler->raw(" = new twig.Markup(sb.toString());\n")->write("sb = {$captureStringBuffer}"); } else { $compiler->raw(' = '); if ($node->getAttribute('safe')) { $compiler->raw("new twig.Markup(")->subcompile($node->getNode('values'))->raw(")"); } else { $compiler->subcompile($node->getNode('values')); } } $compiler->raw(";\n"); $this->count = $count; }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_ExtensionReference) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_ExtensionReference, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node)->write(sprintf("this.env_.getExtension(%s);\n", json_encode($node->getAttribute('name')))); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Spaceless) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Spaceless, but got "%s".', get_class($node))); } $count = $this->count++; $sbName = 'slSb' . ($count > 0 ? $count : ''); $compiler->addDebugInfo($node)->write("var {$sbName} = sb;\n")->write("sb = new twig.StringBuffer;")->subcompile($node->getNode('body'))->write("{$sbName}.append(twig.spaceless(sb.toString()));\n")->write("sb = {$sbName};\n"); $this->count = $count; }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Import) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Import, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node)->write("")->subcompile($node->getNode('var'))->raw(' = '); if ($node->getNode('expr') instanceof Twig_Node_Expression_Name && '_self' === $node->getNode('expr')->getAttribute('name')) { $compiler->raw("this"); } else { $compiler->raw('this.env_.createTemplate(')->setTemplateName(true)->subcompile($node->getNode('expr'))->setTemplateName(false)->raw(")"); } $compiler->raw(";\n"); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Parent) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Parent, but got "%s".', get_class($node))); } if ($output = $node->getAttribute('output')) { $compiler->addDebugInfo($node)->write("sb.append("); } $compiler->raw("this.renderParentBlock(")->string($node->getAttribute('name'))->raw(", context, blocks)"); if ($output) { $compiler->raw(");\n"); } }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Include) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Include, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node); // Is there are use case for conditional includes at runtime? // if ($node->getAttribute('ignore_missing')) { // $compiler // ->write("try {\n") // ->indent() // ; // } $compiler->isTemplateName = true; if ($node->getNode('expr') instanceof Twig_Node_Expression_Constant) { $compiler->write("(new ")->subcompile($node->getNode('expr'))->raw("(this.env_)).render_(sb, "); } else { $compiler->write("(new ")->subcompile($node->getNode('expr'))->raw("(this.env_)).render_(sb, "); } $compiler->isTemplateName = false; if (false === $node->getAttribute('only')) { if (null === $node->getNode('variables')) { $compiler->raw('context'); } else { $compiler->raw('twig.extend({}, context, ')->subcompile($node->getNode('variables'))->raw(')'); } } else { if (null === $node->getNode('variables')) { $compiler->raw('{}'); } else { $compiler->subcompile($node->getNode('variables')); } } $compiler->raw(");\n"); // if ($node->getAttribute('ignore_missing')) { // $compiler // ->outdent() // ->write("} catch (Twig_Error_Loader \$e) {\n") // ->indent() // ->write("// ignore missing template\n") // ->outdent() // ->write("}\n\n") // ; // } }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_If) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\If, but got "%s".', get_class($node))); } $compiler->addDebugInfo($node); for ($i = 0; $i < count($node->getNode('tests')); $i += 2) { if ($i > 0) { $compiler->outdent()->write("} else if ("); } else { $compiler->write('if ('); } $compiler->subcompile($node->getNode('tests')->getNode($i))->raw(") {\n")->indent()->subcompile($node->getNode('tests')->getNode($i + 1)); } if ($node->hasNode('else') && null !== $node->getNode('else')) { $compiler->outdent()->write("} else {\n")->indent()->subcompile($node->getNode('else')); } $compiler->outdent()->write("}\n"); }
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(); }