/**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler
         ->addDebugInfo($this)
         ->write(sprintf("\$this->displayBlock('%s', \$context, \$blocks);\n", $this->getAttribute('name')))
     ;
 }
Example #2
0
    /**
     * Compiles the node to PHP.
     *
     * @param Twig_Compiler A Twig_Compiler instance
     */
    public function compile(Twig_Compiler $compiler)
    {
        $arguments = array();
        foreach ($this->getNode('arguments') as $argument) {
            $arguments[] = '$'.$argument->getAttribute('name').' = null';
        }

        $compiler
            ->addDebugInfo($this)
            ->write(sprintf("public function get%s(%s)\n", $this->getAttribute('name'), implode(', ', $arguments)), "{\n")
            ->indent()
            ->write("\$context = array_merge(\$this->env->getGlobals(), array(\n")
            ->indent()
        ;

        foreach ($this->getNode('arguments') as $argument) {
            $compiler
                ->write('')
                ->string($argument->getAttribute('name'))
                ->raw(' => $'.$argument->getAttribute('name'))
                ->raw(",\n")
            ;
        }

        $compiler
            ->outdent()
            ->write("));\n\n")
            ->write("ob_start();\n")
            ->subcompile($this->getNode('body'))
            ->raw("\n")
            ->write("return ob_get_clean();\n")
            ->outdent()
            ->write("}\n\n")
        ;
    }
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $template = $this->getNode('expr')->getAttribute('value');
     $value = 'Elements' . DS . $template . '.tpl';
     $this->getNode('expr')->setAttribute('value', $value);
     if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
         $compiler->write("\$this->env->loadTemplate(")->subcompile($this->getNode('expr'))->raw(")->display(");
     } else {
         $compiler->write("\$template = ")->subcompile($this->getNode('expr'))->raw(";\n")->write("if (!\$template")->raw(" instanceof Twig_Template) {\n")->indent()->write("\$template = \$this->env->loadTemplate(\$template);\n")->outdent()->write("}\n")->write('$template->display(');
     }
     if (false === $this->getAttribute('only')) {
         if (null === $this->getNode('variables')) {
             $compiler->raw('$context');
         } else {
             $compiler->raw('array_merge($context, ')->subcompile($this->getNode('variables'))->raw(')');
         }
     } else {
         if (null === $this->getNode('variables')) {
             $compiler->raw('array()');
         } else {
             $compiler->subcompile($this->getNode('variables'));
         }
     }
     $compiler->raw(");\n");
 }
Example #4
0
 /**
  * compile
  *
  * @param \Twig_Compiler $compiler
  *
  * @return  void
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $data = $this->getNode('value')->getAttribute('data');
     $markdown = new MarkdownExtra();
     $data = $markdown->defaultTransform($data);
     $compiler->write('echo ')->string($data)->raw(';');
 }
Example #5
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $vars = $this->getNode('vars');
     $defaults = new \Twig_Node_Expression_Array(array(), -1);
     if ($vars instanceof \Twig_Node_Expression_Array) {
         $defaults = $this->getNode('vars');
         $vars = null;
     }
     list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults);
     $method = null === $this->getNode('count') ? 'trans' : 'transChoice';
     $compiler->write('echo $this->env->getExtension(\'translation\')->getTranslator()->' . $method . '(')->subcompile($msg);
     $compiler->raw(', ');
     if (null !== $this->getNode('count')) {
         $compiler->subcompile($this->getNode('count'))->raw(', ');
     }
     if (null !== $vars) {
         $compiler->raw('array_merge(')->subcompile($defaults)->raw(', ')->subcompile($this->getNode('vars'))->raw(')');
     } else {
         $compiler->subcompile($defaults);
     }
     $compiler->raw(', ')->subcompile($this->getNode('domain'));
     if (null !== $this->getNode('locale')) {
         $compiler->raw(', ')->subcompile($this->getNode('locale'));
     }
     $compiler->raw(");\n");
 }
 public function compile(\Twig_Compiler $compiler)
 {
     $varsToUnset = array();
     // Rights verification
     $compiler->write('$allowed = true; ' . "\n");
     foreach ($this->getAttribute('paths') as $path) {
         $compiler->write('$allowed = $context[\'dcylabs_twig_serviceProvider\']->get(\'path_roles\')->checkPath( ' . "\n")->subcompile($path)->write(') ? $allowed : false ;' . "\n");
     }
     $compiler->write('if($allowed){ ' . "\n");
     // Variable generation
     if (count($this->getAttribute('paths')) > 1) {
         foreach ($this->getAttribute('paths') as $path) {
             $this->setVar($compiler, '$context[\'check_urls\'][]', $path);
         }
         array_push($varsToUnset, 'check_urls');
     }
     $this->setVar($compiler, '$context[\'check_url\']', $this->getAttribute('paths')[0]);
     array_push($varsToUnset, 'check_url');
     $compiler->subcompile($this->getAttribute('body'));
     if (!is_null($this->getAttribute('alternativeBody'))) {
         $compiler->write('} else { ' . "\n");
         $compiler->subcompile($this->getAttribute('alternativeBody'));
     }
     $compiler->write('} ' . "\n");
     foreach ($varsToUnset as $var) {
         $this->unsetVar($compiler, '$context[\'' . $var . '\']');
     }
 }
Example #7
0
 public function compile(Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     $testMap = $compiler->getEnvironment()->getTests();
     if (!isset($testMap[$name])) {
         $message = sprintf('The test "%s" does not exist', $name);
         if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getTests()))) {
             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
         }
         throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename());
     }
     $name = $this->getAttribute('name');
     $node = $this->getNode('node');
     $compiler->raw($testMap[$name]->compile() . '(')->subcompile($node);
     if (null !== $this->getNode('arguments')) {
         $compiler->raw(', ');
         $max = count($this->getNode('arguments')) - 1;
         foreach ($this->getNode('arguments') as $i => $arg) {
             $compiler->subcompile($arg);
             if ($i != $max) {
                 $compiler->raw(', ');
             }
         }
     }
     $compiler->raw(')');
 }
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->write('echo($this->env->getExtension(\'list_script_extension\')->get(
             \'' . $this->getAttribute('namespace') . '\',
             \'' . $this->getAttribute('bundle') . '\',
             \'' . $this->getAttribute('controller') . '\'));');
 }
Example #9
0
 private function compileTemplateCall(Twig_Compiler $compiler)
 {
     if (!$this->hasNode('template')) {
         return $compiler->write('$this');
     }
     return $compiler->write('$this->loadTemplate(')->subcompile($this->getNode('template'))->raw(', ')->repr($this->getTemplateName())->raw(', ')->repr($this->getTemplateLine())->raw(')');
 }
Example #10
0
 public function compile(\Twig_Compiler $compiler)
 {
     if (!$this->getNode('right') instanceof \Twig_Node_Expression_Name) {
         throw new \Exception("Right side of const operator must be identifier.");
     }
     $compiler->raw('(constant(get_class(')->subcompile($this->getNode('left'))->raw(') . (')->string("::" . $this->getNode('right')->getAttribute('name'))->raw(')))');
 }
Example #11
0
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $type = $this->getAttribute('type');
     $template = $type == \Wave\View\Tag\Register::TYPE_JS ? self::FORMAT_JS : self::FORMAT_CSS;
     $compiler->write('foreach($this->env->_wave_register["' . $type . '"] as $priority => $files):')->raw("\n\t")->write('foreach($files as $file => $extra):')->raw("\n\t")->write('$code = sprintf("' . $template . '", $file, $extra);')->raw("\n\t")->write('echo $code . "<!-- $priority -->\\n";')->raw("\n\t")->write('endforeach;')->raw("\n")->write('endforeach;')->raw("\n");
 }
 public function compile(Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     if (false === ($function = $compiler->getEnvironment()->getFunction($name))) {
         $message = sprintf('The function "%s" does not exist', $name);
         if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFunctions()))) {
             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
         }
         throw new Twig_Error_Syntax($message, $this->getLine());
     }
     $compiler->raw($function->compile() . '(')->raw($function->needsEnvironment() ? '$this->env' : '');
     if ($function->needsContext()) {
         $compiler->raw($function->needsEnvironment() ? ', $context' : '$context');
     }
     $first = true;
     foreach ($this->getNode('arguments') as $node) {
         if (!$first) {
             $compiler->raw(', ');
         } else {
             if ($function->needsEnvironment() || $function->needsContext()) {
                 $compiler->raw(', ');
             }
             $first = false;
         }
         $compiler->subcompile($node);
     }
     $compiler->raw(')');
 }
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $attrib = $this->getAttribute('name');
     //        if ($attrib == 'ajax') {
     $compiler->write('echo "<script type=\\"text/template\\" id=\\"ebussola-statefull-message-template\\">";')->write('$context["type"] = \'{{ type }}\';')->write('$context["message"] = \'{{ message }}\';')->subcompile($this->getNode('body'))->write('echo "</script>";');
     //        }
 }
Example #14
0
 public function compile(Twig_Compiler $compiler)
 {
     $name = $this->getAttribute('name');
     if ($this->getAttribute('is_defined_test')) {
         if ($this->isSpecial()) {
             $compiler->repr(true);
         } else {
             $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)');
         }
     } elseif ($this->isSpecial()) {
         $compiler->raw($this->specialVars[$name]);
     } elseif ($this->getAttribute('always_defined')) {
         $compiler->raw('$context[')->string($name)->raw(']');
     } else {
         // remove the non-PHP 5.4 version when PHP 5.3 support is dropped
         // as the non-optimized version is just a workaround for slow ternary operator
         // when the context has a lot of variables
         if (version_compare(phpversion(), '5.4.0RC1', '>=') && ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables())) {
             // PHP 5.4 ternary operator performance was optimized
             $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : null)');
         } else {
             $compiler->raw('$this->getContext($context, ')->string($name);
             if ($this->getAttribute('ignore_strict_check')) {
                 $compiler->raw(', true');
             }
             $compiler->raw(')');
         }
     }
 }
Example #15
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("\$context['_parent'] = (array) \$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");
 }
Example #16
0
 public function compile(Twig_Compiler $compiler)
 {
     if (function_exists('twig_template_get_attributes')) {
         $compiler->raw('twig_template_get_attributes($this, ');
     } else {
         $compiler->raw('$this->getAttribute(');
     }
     if ($this->getAttribute('ignore_strict_check')) {
         $this->getNode('node')->setAttribute('ignore_strict_check', true);
     }
     $compiler->subcompile($this->getNode('node'));
     $compiler->raw(', ')->subcompile($this->getNode('attribute'));
     if (count($this->getNode('arguments')) || Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
         $compiler->raw(', ')->subcompile($this->getNode('arguments'));
         if (Twig_TemplateInterface::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
             $compiler->raw(', ')->repr($this->getAttribute('type'));
         }
         if ($this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
             $compiler->raw(', ' . ($this->getAttribute('is_defined_test') ? 'true' : 'false'));
         }
         if ($this->getAttribute('ignore_strict_check')) {
             $compiler->raw(', ' . ($this->getAttribute('ignore_strict_check') ? 'true' : 'false'));
         }
     }
     $compiler->raw(')');
 }
Example #17
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write("\$location = ")->subcompile($this->getNode('expr'))->raw(";\n")->write("\$namespace = false;\n")->write("if (strpos(\$location, '@') === 0) {\n")->indent()->write("\$namespace = substr(\$location, 1, strpos(\$location, '/') - 1);\n")->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n")->write("\$this->env->setNamespaceLookUpOrder(array(\$namespace, '__main__'));\n")->outdent()->write("}\n");
     parent::compile($compiler);
     $compiler->write("if (\$namespace) {\n")->indent()->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n")->outdent()->write("}\n");
 }
Example #18
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler
  *
  * @return null
  */
 public function compile(\Twig_Compiler $compiler)
 {
     // Remember what 'nav' was set to before
     $compiler->write("if (isset(\$context['nav'])) {\n")->indent()->write("\$_nav = \$context['nav'];\n")->outdent()->write("}\n");
     parent::compile($compiler);
     $compiler->write("if (isset(\$_thisItemLevel)) {\n")->indent()->write("\$_tmpContext = \$context;\n")->write("if (\$_thisItemLevel > \$_firstItemLevel) {\n")->indent()->write("for (\$_i = \$_thisItemLevel; \$_i > \$_firstItemLevel; \$_i--) {\n")->indent()->write("if (isset(\$_contextsByLevel[\$_i])) {\n")->indent()->write("\$context = \$_contextsByLevel[\$_i];\n")->subcompile($this->navItemNode->getNode('lower_body'), false)->subcompile($this->navItemNode->getNode('outdent'), false)->outdent()->write("}\n")->outdent()->write("}\n")->outdent()->write("}\n")->write("\$context = \$_contextsByLevel[\$_firstItemLevel];\n")->subcompile($this->navItemNode->getNode('lower_body'), false)->write("\$context = \$_tmpContext;\n")->write("unset(\$_thisItemLevel, \$_firstItemLevel, \$_i, \$_contextsByLevel, \$_tmpContext);\n")->outdent()->write("}\n")->write("if (isset(\$_nav)) {\n")->indent()->write("\$context['nav'] = \$_nav;\n")->write("unset(\$_nav);\n")->outdent()->write("}\n");
 }
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     if ($this->getAttribute('as_string')) {
         $compiler->raw('(string) ');
     }
     $compiler->raw("\$this->renderBlock(")->subcompile($this->getNode('name'))->raw(", \$context, \$blocks)");
 }
Example #20
0
 /** {@inheritdoc} */
 public function compile(\Twig_Compiler $compiler)
 {
     foreach ($this->getAttribute('files') as $file) {
         $compiler->write('$context["asset"] = "' . $file . '";');
         $this->nodes[0]->compile($compiler);
     }
 }
 protected function compileDisplayBody(Twig_Compiler $compiler)
 {
     if (null === $this->getNode('parent')) {
         $compiler->write("\$this->checkSecurity();\n");
     }
     parent::compileDisplayBody($compiler);
 }
Example #22
0
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->raw('(')->subcompile($this->getNode('node'))->raw(' === constant(');
     if ($this->getNode('arguments')->hasNode(1)) {
         $compiler->raw('get_class(')->subcompile($this->getNode('arguments')->getNode(1))->raw(')."::".');
     }
     $compiler->subcompile($this->getNode('arguments')->getNode(0))->raw('))');
 }
Example #23
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler
         ->raw("\$this->renderParentBlock(")
         ->string($this->getAttribute('name'))
         ->raw(", \$context, \$blocks)")
     ;
 }
Example #24
0
 public function testCompile()
 {
     $form = new \Twig_Node_Expression_Name('form', 0);
     $resources = new \Twig_Node(array(new \Twig_Node_Expression_Constant('tpl1', 0), new \Twig_Node_Expression_Constant('tpl2', 0)));
     $node = new FormThemeNode($form, $resources, 0);
     $compiler = new \Twig_Compiler(new \Twig_Environment());
     $this->assertEquals(sprintf('echo $this->env->getExtension(\'form\')->setTheme(%s, array("tpl1", "tpl2", ));', $this->getVariableGetter('form')), trim($compiler->compile($node)->getSource()));
 }
Example #25
0
 protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter)
 {
     $compiler->raw($filter->compile() . '(')->raw($filter->needsEnvironment() ? '$this->env, ' : '')->raw($filter->needsContext() ? '$context, ' : '')->subcompile($this->getNode('node'));
     foreach ($this->getNode('arguments') as $node) {
         $compiler->raw(', ')->subcompile($node);
     }
     $compiler->raw(')');
 }
Example #26
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $config = $this->environment->get_phpbb_config();
     $compiler->write("\$asset_file = ")->subcompile($this->getNode('expr'))->raw(";\n")->write("\$asset = new \\phpbb\\template\\asset(\$asset_file, \$this->getEnvironment()->get_path_helper());\n")->write("if (substr(\$asset_file, 0, 2) !== './' && \$asset->is_relative()) {\n")->indent()->write("\$asset_path = \$asset->get_path();")->write("\$local_file = \$this->getEnvironment()->get_phpbb_root_path() . \$asset_path;\n")->write("if (!file_exists(\$local_file)) {\n")->indent()->write("\$local_file = \$this->getEnvironment()->findTemplate(\$asset_path);\n")->write("\$asset->set_path(\$local_file, true);\n")->outdent()->write("\$asset->add_assets_version('{$config['assets_version']}');\n")->write("\$asset_file = \$asset->get_url();\n")->write("}\n")->outdent()->write("}\n")->write("\$context['definition']->append('{$this->get_definition_name()}', '");
     $this->append_asset($compiler);
     $compiler->raw("\n');\n");
 }
Example #27
0
 protected function compileDefaults(\Twig_Compiler $compiler, \Twig_Node_Expression_Array $defaults)
 {
     $compiler->raw('array(');
     foreach ($defaults as $name => $default) {
         $compiler->repr($name)->raw(' => ')->subcompile($default)->raw(', ');
     }
     $compiler->raw(')');
 }
Example #28
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('echo $this->env->getExtension(\'form\')->setTheme(')->subcompile($this->getNode('form'))->raw(', array(');
     foreach ($this->getNode('resources') as $resource) {
         $compiler->subcompile($resource)->raw(', ');
     }
     $compiler->raw("));\n");
 }
Example #29
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(Twig_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)");
     }
 }
Example #30
0
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->raw('$this->getAttribute(')->subcompile($this->getNode('node'))->raw(', ')->subcompile($this->getNode('attribute'))->raw(', array(');
     foreach ($this->getNode('arguments') as $node) {
         $compiler->subcompile($node)->raw(', ');
     }
     $compiler->raw('), ')->repr($this->getAttribute('type'))->raw($this->hasAttribute('is_defined_test') ? ', true' : ', false')->raw(sprintf(', %d', $this->lineno))->raw(')');
 }