Example #1
0
 /**
  * {@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(')');
     }
 }
Example #2
0
 protected function compileGetParent(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     $compiler->write("/**\n", " * @inheritDoc\n", " */\n")->write($this->functionName . ".prototype.getParent_ = function(context) {\n")->indent()->write('return ');
     if (null === $node->getNode('parent')) {
         $compiler->repr(false);
     } else {
         $compiler->setTemplateName(true)->subcompile($node->getNode('parent'))->setTemplateName(false);
     }
     $compiler->raw(";\n")->outdent()->write("};\n\n");
 }
Example #3
0
 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'));
 }