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_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_Expression_Binary_Power) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Binary_Power, but got "%s".', get_class($node))); } $compiler->raw('Math.pow(')->subcompile($this->getNode('left'))->raw(', ')->subcompile($this->getNode('right'))->raw(')'); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Test_Defined) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Test_Defined, but got "%s".', get_class($node))); } $compiler->subcompile(new \Twig_Node_Expression_Test($node->getNode('node'), $node->getAttribute('name'), $node->getNode('arguments'), $node->getLine())); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_AssignName) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_AssignName, but got "%s".', get_class($node))); } $compiler->raw('context[')->string($node->getAttribute('name'))->unsetVar($node->getAttribute('name'))->raw(']'); }
protected function operator(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Binary_NotEqual) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Binary_NotEqual, but got "%s".', get_class($node))); } return $compiler->raw('!='); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_AutoEscape) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\AutoEscape, but got "%s".', get_class($node))); } $compiler->subcompile($node->getNode('body')); }
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_Expression_Conditional) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_Conditional, but got "%s".', get_class($node))); } $compiler->raw('((')->subcompile($node->getNode('expr1'))->raw(') ? (')->subcompile($node->getNode('expr2'))->raw(') : (')->subcompile($node->getNode('expr3'))->raw('))'); }
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_Expression_MacroCall) { throw new \LogicException(sprintf('This compiler does not support the type "%s".', get_class($node))); } $argsNode = $node->getNode('arguments'); if (!$argsNode instanceof \Twig_Node_Expression_Array) { throw new \LogicException('Did not find args node.'); } $namedNames = array(); $namedCount = 0; $positionalCount = 0; foreach ($argsNode->getKeyValuePairs() as $pair) { $name = $pair['key']->getAttribute('value'); if (!is_int($name)) { $namedCount++; $namedNames[$name] = 1; } elseif ($namedCount > 0) { throw new \Twig_Error_Syntax(sprintf('Positional arguments cannot be used after named arguments for macro "%s".', $node->getAttribute('name')), $node->getLine()); } else { $positionalCount++; } } $compiler->raw('this.callMacro(')->subcompile($node->getNode('template'))->raw(', ')->repr($node->getAttribute('name'))->raw(', ')->subcompile($argsNode); if ($namedCount > 0) { $compiler->raw(', ')->repr($namedNames)->raw(', ')->repr($namedCount)->raw(', ')->repr($positionalCount); } $compiler->raw(')'); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_DefaultFilter) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_DefaultFilter, but got "%s".', get_class($node))); } $compiler->subcompile($node->getNode('node')); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Binary_NotIn) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Binary_NotIn, but got "%s".', get_class($node))); } // order of arguments is reversed, see the InCompiler $compiler->raw('!twig.contains(')->subcompile($node->getNode('right'))->raw(', ')->subcompile($node->getNode('left'))->raw(')'); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Unary) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_Unary, but got "%s".', get_class($node))); } $compiler->raw('('); $this->operator($compiler, $node); $compiler->subcompile($node->getNode('node'))->raw(')'); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Binary_In) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Binary_In, but got "%s".', get_class($node))); } // order left,right is reversed in JsCompiler compared to Twig_Compiler // since the twig.contains filter takes reverse arguments to follow // Google Closure conventions $compiler->raw('twig.contains(')->subcompile($node->getNode('right'))->raw(', ')->subcompile($node->getNode('left'))->raw(')'); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_ExtensionReference) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_ExtensionReference, but got "%s".', get_class($node))); } $compiler->raw("this.env_.getExtension(")->subcompile($node->getNode('name'))->raw(")"); if ($output) { $compiler->raw(";\n"); } }
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_Expression_Constant) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_Constant, but got "%s".', get_class($node))); } if ($compiler->isTemplateName) { $env = $compiler->getEnvironment(); $source = $env->getLoader()->getSource($node->getAttribute('value')); $module = $env->parse($env->tokenize($source, $node->getAttribute('value'))); $compiler->raw($compiler->getFunctionName($module)); return; } $compiler->repr($node->getAttribute('value')); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_ForLoop) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_ForLoop, but got "%s".', get_class($node))); } if ($node->getAttribute('else')) { $compiler->write("")->subcompile(new \Twig_Node_Expression_Name('_iterated', $node->getLine()))->raw(" = true;\n"); } if ($node->getAttribute('with_loop')) { $compiler->write("++")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['index0'];\n")->write("++")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['index'];\n")->write("")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['first'] = false;\n"); if (!$node->getAttribute('ifexpr')) { $compiler->write("if (")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['length']) {\n")->indent()->write("--")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['revindex0'];\n")->write("--")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['revindex'];\n")->write("")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['last'] = 0 === ")->subcompile(new \Twig_Node_Expression_Name('loop', $node->getLine()))->raw("['revindex0'];\n")->outdent()->write("}\n"); } } }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_MethodCall) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_MethodCall, but got "%s".', get_class($node))); } $compiler->subcompile($node->getNode('node'))->raw('.')->raw($node->getAttribute('method'))->raw('('); $first = true; foreach ($node->getNode('arguments')->getKeyValuePairs() as $pair) { if (!$first) { $compiler->raw(', '); } $first = false; $compiler->subcompile($pair['value']); } $compiler->raw(')'); }
protected function compileClassHeader(JsCompiler $compiler, Twig_NodeInterface $node) { $this->functionName = $functionName = $compiler->templateFunctionName = $compiler->getFunctionName($node); $parts = explode('.', $functionName); array_pop($parts); $filename = $node->getAttribute('filename'); if (!empty($filename) && false !== strpos($filename, DIRECTORY_SEPARATOR)) { $parts = explode(DIRECTORY_SEPARATOR, realpath($filename)); $filename = implode(DIRECTORY_SEPARATOR, array_splice($parts, -4)); } $compiler->write("/**\n")->write(" * @fileoverview Compiled template for file\n")->write(" *\n")->write(" * " . str_replace('*/', '*\\/', $filename) . "\n")->write(" *\n")->write(" * @suppress {checkTypes|fileoverviewTags}\n")->write(" */\n")->write("\n"); $compiler->write("goog.provide('{$functionName}');\n")->write("\n")->write("goog.require('twig');\n")->write("goog.require('twig.filter');\n")->write("\n")->write("/**\n", " * @constructor\n", " * @param {twig.Environment} env\n", " * @extends {twig.Template}\n", " */\n")->write("{$functionName} = function(env) {\n")->indent()->write("twig.Template.call(this, env);\n"); if (count($node->getNode('blocks')) || count($node->getNode('traits'))) { $this->compileConstructor($compiler, $node); } $compiler->outdent()->write("};\n")->write("twig.inherits({$functionName}, twig.Template);\n\n"); }
public function __construct(\Twig_Environment $env) { parent::__construct($env); $this->addTypeCompiler(new GetAttrCompiler()); $this->addTypeCompiler(new NameCompiler()); $this->addTypeCompiler(new RequireModuleCompiler()); $this->addTestCompiler(new SameAsCompiler()); $this->addTypeCompiler(new MacroCompiler()); $this->addTypeCompiler(new ForCompiler()); }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_TempName) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_TempName, but got "%s".', get_class($node))); } $name = $node->getAttribute('name'); if (isset($compiler->localVarMap[$name])) { $compiler->raw($compiler->localVarMap[$name]); return; } /* Contrary to the name of this class, this code no longer compiles the name of the node to a temporary variable name. Because of the troubles resulting from this optimization reported in schmittjoh/twig.js#12, this has been replaced with the slightly less performant but more reliable NameCompiler output which reads the variable directly from the context. */ $compiler->raw('(')->string($name)->raw(' in context ? context[')->string($name)->raw('] : null)'); }
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") // ; // } }
/** * {@inheritdoc} */ public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Name) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_Name, but got "%s".', get_class($node))); } $name = $node->getAttribute('name'); if ($node->getAttribute('is_defined_test')) { if ($node->isSpecial()) { $compiler->repr(true); } else { $compiler->raw('(')->repr($name)->raw(' in context)'); } } elseif ($node->isSpecial()) { static $specialVars = ['_self' => 'this', '_context' => 'context', '_charset' => 'this.env_.getCharset()']; if (!isset($specialVars[$name])) { throw new \RuntimeException(sprintf('The special var "%s" is not supported by the NameCompiler.', $name)); } $compiler->raw($specialVars[$name]); } else { if (isset($compiler->localVarMap[$name])) { $compiler->raw($compiler->localVarMap[$name]); return; } // FIXME: Add strict behavior? // see Template::getContext() $compiler->raw('(')->string($name)->raw(' in context ? context[')->string($name)->raw('] : null')->raw(')'); } }
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) { if (!$node instanceof \Twig_Node_Expression_Filter) { throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Filter, but got "%s".', get_class($node))); } $name = $node->getNode('filter')->getAttribute('value'); if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) { throw new \Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $node->getLine()); } if (($filterCompiler = $compiler->getFilterCompiler($name)) && false !== $filterCompiler->compile($compiler, $node)) { return; } elseif ($functionName = $compiler->getFilterFunction($name)) { $compiler->raw($functionName . '('); } else { $compiler->raw('this.env_.filter(')->string($name)->raw(', '); } $compiler->raw($filter->needsEnvironment() ? 'this.env_, ' : '')->raw($filter->needsContext() ? 'context, ' : '')->subcompile($node->getNode('node')); foreach ($node->getNode('arguments') as $subNode) { $compiler->raw(', ')->subcompile($subNode); } $compiler->raw(')'); }
public function compile(JsCompiler $compiler, \Twig_Node_Expression_Filter $node) { if (!($locale = $compiler->getDefine('locale')) || !$this->translator instanceof Translator) { return false; } // unfortunately, the Translation component does not provide a better // way to retrieve these $this->loadCatalogueRef = new \ReflectionMethod($this->translator, 'loadCatalogue'); $this->loadCatalogueRef->setAccessible(true); $this->catalogueRef = new \ReflectionProperty($this->translator, 'catalogues'); $this->catalogueRef->setAccessible(true); // ignore dynamic messages, we cannot resolve these // users can still apply a runtime trans filter to do this $subNode = $node->getNode('node'); if (!$subNode instanceof \Twig_Node_Expression_Constant) { return false; } $id = $subNode->getAttribute('value'); $domain = 'messages'; $hasParams = false; $arguments = $node->getNode('arguments'); if (count($arguments) > 0) { $hasParams = count($arguments->getNode(0)) > 0; if ($arguments->hasNode(1)) { $domainNode = $arguments->getNode(1); if (!$domainNode instanceof \Twig_Node_Expression_Constant) { return false; } $domain = $domainNode->getAttribute('value'); } } $catalogue = $this->getCatalogue($locale); if (!$hasParams) { $compiler->string($catalogue->get($id, $domain)); return; } $compiler->raw('twig.filter.replace(')->string($catalogue->get($id, $domain))->raw(", ")->subcompile($arguments->getNode(0))->raw(')'); }