コード例 #1
0
ファイル: Test.php プロジェクト: bobseven/Slim-Blog
 public function compile(Twig_Compiler $compiler)
 {
     $testMap = $compiler->getEnvironment()->getTests();
     if (!isset($testMap[$this->getAttribute('name')])) {
         throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
     }
     $name = $this->getAttribute('name');
     $node = $this->getNode('node');
     // defined is a special case
     if ('defined' === $name) {
         if ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr) {
             $node->setAttribute('is_defined_test', true);
             $compiler->subcompile($node);
             $node->removeAttribute('is_defined_test');
         } else {
             throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine());
         }
         return;
     }
     $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(')');
 }
コード例 #2
0
 public function compile(Twig_Compiler $compiler)
 {
     $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(', array(');
         foreach ($this->getNode('arguments') as $node) {
             $compiler->subcompile($node)->raw(', ');
         }
         $compiler->raw(')');
         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(')');
 }
コード例 #3
0
ファイル: Cell.php プロジェクト: luisfredgs/TwigView
 public function compile(\Twig_Compiler $compiler)
 {
     // @codingStandardsIgnoreEnd
     $compiler->addDebugInfo($this);
     if ($this->assign) {
         $compiler->raw('$context[\'' . $this->getAttribute('variable') . '\'] = ');
         // @codingStandardsIgnoreStart
     } else {
         // @codingStandardsIgnoreEnd
         $compiler->raw('echo ');
     }
     $compiler->raw('$context[\'_view\']->cell(');
     $compiler->subcompile($this->getNode('name'));
     $data = $this->getNode('data');
     if ($data !== null) {
         $compiler->raw(',');
         $compiler->subcompile($data);
     }
     $options = $this->getNode('options');
     if ($data !== null) {
         $compiler->raw(',');
         $compiler->subcompile($options);
     }
     $compiler->raw(");\n");
 }
コード例 #4
0
 public function compile(Twig_Compiler $compiler)
 {
     $testMap = $compiler->getEnvironment()->getTests();
     if (!isset($testMap[$this->getAttribute('name')])) {
         throw new Twig_Error_Syntax(sprintf('The test "%s" does not exist', $this->getAttribute('name')), $this->getLine());
     }
     $name = $this->getAttribute('name');
     $node = $this->getNode('node');
     // defined is a special case
     if ('defined' === $name) {
         $compiler->subcompile($node);
         return;
     }
     $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(')');
 }
コード例 #5
0
ファイル: AreaNode.php プロジェクト: hexmedia/content-bundle
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $languageNode = $this->getNode("language");
     $tagNode = $this->getNode("tag");
     $classNode = $this->getNode("class");
     $compiler->write('echo($this->env->getExtension(\'content_area_extension\')->get(\'' . $this->getAttribute('name') . '\', ');
     if ($tagNode instanceof \Twig_Node) {
         $compiler->subcompile($tagNode);
     } else {
         $compiler->write("'div'");
     }
     $compiler->write(", ");
     if ($classNode instanceof \Twig_Node) {
         $compiler->subcompile($classNode);
     } else {
         $compiler->write("''");
     }
     $compiler->write(", ");
     $compiler->subcompile($this->compileString($this->getNode("content")));
     $compiler->write(", ");
     $compiler->write($this->getAttribute("isGlobal") ? 'true' : 'false');
     if ($languageNode instanceof \Twig_Node) {
         $compiler->write(", ");
         $compiler->subcompile($languageNode);
     }
     $compiler->write("));");
 }
コード例 #6
0
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $classNode = $this->getNode("class");
     $tagNode = $this->getNode("tag");
     $languageNode = $this->getNode("language");
     $compiler->write('echo($this->env->getExtension(\'content_extension\')->get(
                         $context[\'' . $this->getAttribute('var') . '\'],
             \'' . $this->getAttribute('type') . '\',
             \'' . $this->getAttribute('field') . '\'');
     if ($tagNode instanceof \Twig_Node) {
         $compiler->write(",");
         $compiler->subcompile($tagNode);
     } else {
         $compiler->write(", null");
     }
     if ($classNode instanceof \Twig_Node) {
         $compiler->write(",");
         $compiler->subcompile($classNode);
     } else {
         $compiler->write(", null");
     }
     if ($languageNode instanceof \Twig_Node) {
         $compiler->write(",");
         $compiler->subcompile($languageNode);
     } else {
         $compiler->write(", null");
     }
     $compiler->write('));');
 }
コード例 #7
0
ファイル: TransNode.php プロジェクト: neteasy-work/hkgbf_crm
 /**
  * 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, (bool) $vars);
     $method = null === $this->getNode('count') ? 'trans' : 'transChoice';
     $compiler->write('echo $this->env->getExtension(\'translator\')->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(', ');
     if (null === $this->getNode('domain')) {
         $compiler->repr('messages');
     } else {
         $compiler->subcompile($this->getNode('domain'));
     }
     if (null !== $this->getNode('locale')) {
         $compiler->raw(', ')->subcompile($this->getNode('locale'));
     }
     $compiler->raw(");\n");
 }
コード例 #8
0
ファイル: Set.php プロジェクト: n0way/Twig
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     /*
      * Optimizes the node when capture is used for a large block of text.
      *
      * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
      */
     $safe = false;
     $values = $this->getNode('values');
     if ($this->getAttribute('capture') && $values instanceof Twig_Node_Text) {
         $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine()));
         $this->setAttribute('capture', false);
         $safe = true;
     }
     $compiler->addDebugInfo($this);
     if (count($this->getNode('names')) > 1) {
         $compiler->write('list(');
         foreach ($this->getNode('names') as $idx => $node) {
             if ($idx) {
                 $compiler->raw(', ');
             }
             $compiler->subcompile($node);
         }
         $compiler->raw(')');
     } else {
         if ($this->getAttribute('capture')) {
             $compiler->write("ob_start();\n")->subcompile($this->getNode('values'));
         }
         $compiler->subcompile($this->getNode('names'), false);
         if ($this->getAttribute('capture')) {
             $compiler->raw(" = new Twig_Markup(ob_get_clean())");
         }
     }
     if (!$this->getAttribute('capture')) {
         $compiler->raw(' = ');
         if (count($this->getNode('names')) > 1) {
             $compiler->write('array(');
             foreach ($this->getNode('values') as $idx => $value) {
                 if ($idx) {
                     $compiler->raw(', ');
                 }
                 $compiler->subcompile($value);
             }
             $compiler->raw(')');
         } else {
             if ($safe) {
                 $compiler->raw("new Twig_Markup(")->subcompile($this->getNode('values'))->raw(")");
             } else {
                 $compiler->subcompile($this->getNode('values'));
             }
         }
     }
     $compiler->raw(";\n");
 }
コード例 #9
0
ファイル: WhileNode.php プロジェクト: free2er/twig-extensions
 /**
  * Собирает PHP-выражение
  *
  * @param TwigCompiler $compiler Компилятор Twig.
  *
  * @return void
  */
 public function compile(TwigCompiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('while (');
     $compiler->subcompile($this->getNode('condition'));
     $compiler->write(") {\n");
     $compiler->indent();
     $compiler->subcompile($this->getNode('body'));
     $compiler->outdent();
     $compiler->write("}\n");
 }
コード例 #10
0
 /**
  * @param \Twig_Compiler $compiler
  *
  * @return null
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $n = static::$_cacheCount++;
     $conditions = $this->getNode('conditions');
     $ignoreConditions = $this->getNode('ignoreConditions');
     $key = $this->getNode('key');
     $durationNum = $this->getAttribute('durationNum');
     $durationUnit = $this->getAttribute('durationUnit');
     $expiration = $this->getNode('expiration');
     $global = $this->getAttribute('global') ? 'true' : 'false';
     $flags = $this->getNode('flags');
     $compiler->addDebugInfo($this)->write("\$cacheService = \\Craft\\craft()->templateCache;\n")->write("\$cacheFlagService = \\Craft\\craft()->cacheFlag_cache;\n")->write("\$ignoreCacheTag{$n} = (\\Craft\\craft()->request->isLivePreview() || \\Craft\\craft()->request->getToken()");
     if ($conditions) {
         $compiler->raw(' || !(')->subcompile($conditions)->raw(')');
     } else {
         if ($ignoreConditions) {
             $compiler->raw(' || (')->subcompile($ignoreConditions)->raw(')');
         }
     }
     $compiler->raw(");\n")->write("if (!\$ignoreCacheTag{$n}) {\n")->indent()->write("\$cacheKey{$n} = ");
     if ($key) {
         $compiler->subcompile($key);
     } else {
         $compiler->raw('"' . StringHelper::randomString() . '"');
     }
     $compiler->raw(";\n")->write("\$cacheBody{$n} = \$cacheService->getTemplateCache(\$cacheKey{$n}, {$global});\n")->outdent()->write("}\n")->write("if (empty(\$cacheBody{$n})) {\n")->indent()->write("ob_start();\n")->subcompile($this->getNode('body'))->write("\$cacheBody{$n} = ob_get_clean();\n")->write("if (!\$ignoreCacheTag{$n}) {\n")->indent()->write("\$cacheService->startTemplateCache(\$cacheKey{$n});\n")->write("\$cacheService->endTemplateCache(\$cacheKey{$n}, {$global}, ");
     if ($durationNum) {
         // So silly that PHP doesn't support "+1 week" http://www.php.net/manual/en/datetime.formats.relative.php
         if ($durationUnit == 'week') {
             if ($durationNum == 1) {
                 $durationNum = 7;
                 $durationUnit = 'days';
             } else {
                 $durationUnit = 'weeks';
             }
         }
         $compiler->raw("'+{$durationNum} {$durationUnit}'");
     } else {
         $compiler->raw('null');
     }
     $compiler->raw(', ');
     if ($expiration) {
         $compiler->subcompile($expiration);
     } else {
         $compiler->raw('null');
     }
     $compiler->raw(", \$cacheBody{$n});\n")->outdent()->write("}\n")->outdent();
     if ($flags) {
         $compiler->write("\$cacheFlagService->addCacheByKey(\$cacheKey{$n}, ");
         $compiler->subcompile($flags);
         $compiler->write(");\n");
     }
     $compiler->write("}\n")->write("echo \$cacheBody{$n};\n");
 }
コード例 #11
0
 /**
  * {@inheritdoc}
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $options = $this->getNode('options');
     list($singular, $tokens) = $this->compileString($this->getNode('body'));
     $plural = NULL;
     if (NULL !== $this->getNode('plural')) {
         list($plural, $pluralTokens) = $this->compileString($this->getNode('plural'));
         $tokens = array_merge($tokens, $pluralTokens);
     }
     // Start writing with the function to be called.
     $compiler->write('echo ' . (empty($plural) ? 't' : '\\Drupal::translation()->formatPlural') . '(');
     // Move the count to the beginning of the parameters list.
     if (!empty($plural)) {
         $compiler->raw('abs(')->subcompile($this->getNode('count'))->raw('), ');
     }
     // Write the singular text parameter.
     $compiler->subcompile($singular);
     // Write the plural text parameter, if necessary.
     if (!empty($plural)) {
         $compiler->raw(', ')->subcompile($plural);
     }
     // Write any tokens found as an associative array parameter, otherwise just
     // leave as an empty array.
     $compiler->raw(', array(');
     foreach ($tokens as $token) {
         $compiler->string($token->getAttribute('placeholder'))->raw(' => ')->subcompile($token)->raw(', ');
     }
     $compiler->raw(')');
     // Write any options passed.
     if (!empty($options)) {
         $compiler->raw(', ')->subcompile($options);
     }
     // Write function closure.
     $compiler->raw(')');
     // Append translation debug markup, if necessary.
     if ($compiler->getEnvironment()->isDebug()) {
         $compiler->raw(" . '\n<!-- TRANSLATION: ");
         $compiler->subcompile($singular);
         if (!empty($plural)) {
             $compiler->raw(', PLURAL: ')->subcompile($plural);
         }
         if (!empty($options)) {
             foreach ($options->getKeyValuePairs() as $pair) {
                 $compiler->raw(', ' . Unicode::strtoupper($pair['key']->getAttribute('value')) . ': ')->subcompile($pair['value']);
             }
         }
         $compiler->raw(" -->\n'");
     }
     // End writing.
     $compiler->raw(";\n");
 }
コード例 #12
0
ファイル: MethodCall.php プロジェクト: jothatron/xvwa
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->subcompile($this->getNode('node'))->raw('->')->raw($this->getAttribute('method'))->raw('(');
     $first = true;
     foreach ($this->getNode('arguments')->getKeyValuePairs() as $pair) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $first = false;
         $compiler->subcompile($pair['value']);
     }
     $compiler->raw(')');
 }
コード例 #13
0
ファイル: GetAttr.php プロジェクト: bobseven/Slim-Blog
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->raw('$this->getAttribute(');
     if ($this->hasAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) {
         $compiler->subcompile(new Twig_Node_Expression_Filter($this->getNode('node'), new Twig_Node_Expression_Constant('default', $this->getLine()), new Twig_Node(), $this->getLine()));
     } else {
         $compiler->subcompile($this->getNode('node'));
     }
     $compiler->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(')');
 }
コード例 #14
0
ファイル: Set.php プロジェクト: nizsheanez/PolymorphCMS
    /**
     * Compiles the node to PHP.
     *
     * @param Twig_Compiler A Twig_Compiler instance
     */
    public function compile(Twig_Compiler $compiler)
    {
        $compiler->addDebugInfo($this);

        if (count($this->getNode('names')) > 1) {
            $compiler->write('list(');
            foreach ($this->getNode('names') as $idx => $node) {
                if ($idx) {
                    $compiler->raw(', ');
                }

                $compiler->subcompile($node);
            }
            $compiler->raw(')');
        } else {
            if ($this->getAttribute('capture')) {
                $compiler
                    ->write("ob_start();\n")
                    ->subcompile($this->getNode('values'))
                ;
            }

            $compiler->subcompile($this->getNode('names'), false);

            if ($this->getAttribute('capture')) {
                $compiler->raw(" = new Twig_Markup(ob_get_clean())");
            }
        }

        if (!$this->getAttribute('capture')) {
            $compiler->raw(' = ');

            if (count($this->getNode('names')) > 1) {
                $compiler->write('array(');
                foreach ($this->getNode('values') as $idx => $value) {
                    if ($idx) {
                        $compiler->raw(', ');
                    }

                    $compiler->subcompile($value);
                }
                $compiler->raw(')');
            } else {
                $compiler->subcompile($this->getNode('values'));
            }
        }

        $compiler->raw(";\n");
    }
コード例 #15
0
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $arguments = iterator_to_array($this->getNode('arguments'));
     $compiler->write('$this->env->getExtension(\'form\')->renderer->renderBlock(');
     if (isset($arguments[0])) {
         $compiler->subcompile($arguments[0]);
         $compiler->raw(', \'' . $this->getAttribute('name') . '\'');
         if (isset($arguments[1])) {
             $compiler->raw(', ');
             $compiler->subcompile($arguments[1]);
         }
     }
     $compiler->raw(')');
 }
コード例 #16
0
ファイル: GetAttr.php プロジェクト: jesusmarket/jesusmarket
 public function compile(Twig_Compiler $compiler)
 {
     if (function_exists('twig_template_get_attributes') && !$this->getAttribute('disable_c_ext')) {
         $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_Template::ANY_CALL !== $this->getAttribute('type') || $this->getAttribute('is_defined_test') || $this->getAttribute('ignore_strict_check')) {
         $compiler->raw(', ')->subcompile($this->getNode('arguments'));
         if (Twig_Template::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(')');
 }
コード例 #17
0
ファイル: Function.php プロジェクト: necromuncher/Twig
 public function compile(Twig_Compiler $compiler)
 {
     $function = $compiler->getEnvironment()->getFunction($this->getAttribute('name'));
     $compiler->raw($function->compile() . '(');
     $first = true;
     if ($function->needsEnvironment()) {
         $compiler->raw('$this->env');
         $first = false;
     }
     if ($function->needsContext()) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $compiler->raw('$context');
         $first = false;
     }
     foreach ($function->getArguments() as $argument) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $compiler->string($argument);
         $first = false;
     }
     foreach ($this->getNode('arguments') as $node) {
         if (!$first) {
             $compiler->raw(', ');
         }
         $compiler->subcompile($node);
         $first = false;
     }
     $compiler->raw(')');
 }
コード例 #18
0
ファイル: Function.php プロジェクト: roverwolf/Twig
    public function compile(Twig_Compiler $compiler)
    {
        $function = $compiler->getEnvironment()->getFunction($this->getNode('name')->getAttribute('name'));
        if (!$function) {
            throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getNode('name')->getAttribute('name')), $this->getLine());
        }

        $compiler
            ->raw($function->compile().'(')
            ->raw($function->needsEnvironment() ? '$this->env, ' : '')
            ->raw($function->needsContext() ? '$context, ' : '')
        ;

        $first = true;
        foreach ($this->getNode('arguments') as $node) {
            if (!$first) {
                $compiler->raw(', ');
            } else {
                $first = false;
            }
            $compiler->subcompile($node);
        }

        $compiler->raw(')');
    }
コード例 #19
0
 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(')');
 }
コード例 #20
0
 public function compile(\Twig_Compiler $compiler)
 {
     $count = count($this->getNode('params'));
     if ($count == 1) {
         throw new \Twig_Error_Runtime('Content identifier is missing.');
     } else {
         if ($count > 2) {
             throw new \Twig_Error_Runtime('Too many arguments.');
         }
     }
     $compiler->addDebugInfo($this);
     for ($i = 0; $i < $count; $i++) {
         // argument is not an expression (such as, a \Twig_Node_Textbody)
         // we should trick with output buffering to get a valid argument to pass
         // to the functionToCall() function.
         if (!$this->getNode('params')->getNode($i) instanceof \Twig_Node_Expression) {
             $compiler->write('ob_start();')->raw(PHP_EOL);
             $compiler->subcompile($this->getNode('params')->getNode($i));
             $compiler->write('$_content[] = ob_get_clean();')->raw(PHP_EOL);
         } else {
             $compiler->write('$_content[] = ')->subcompile($this->getNode('params')->getNode($i))->raw(';')->raw(PHP_EOL);
         }
     }
     $rawText = $this->retriever->render($this->getNode('params')->getNode(1)->getAttribute('value'), $this->getNode('params')->getNode(0)->getAttribute('data'));
     $compiler->raw('echo "' . str_replace('"', '\\"', $rawText) . '";')->raw(PHP_EOL);
 }
コード例 #21
0
ファイル: Filter.php プロジェクト: jackbravo/symfony-sandbox
 public function compile(Twig_Compiler $compiler)
 {
     $filterMap = $compiler->getEnvironment()->getFilters();
     $name = $this->getNode('filter')->getAttribute('value');
     if (!isset($filterMap[$name])) {
         throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine());
     }
     $filter = $filterMap[$name];
     // The default filter is intercepted when the filtered value
     // is a name (like obj) or an attribute (like obj.attr)
     // In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }}
     if ('default' === $name && ($this->getNode('node') instanceof Twig_Node_Expression_Name || $this->getNode('node') instanceof Twig_Node_Expression_GetAttr)) {
         $compiler->raw('(');
         if ($this->getNode('node') instanceof Twig_Node_Expression_Name) {
             $testMap = $compiler->getEnvironment()->getTests();
             $compiler->raw($testMap['defined']->compile() . '(')->repr($this->getNode('node')->getAttribute('name'))->raw(', $context)');
         } elseif ($this->getNode('node') instanceof Twig_Node_Expression_GetAttr) {
             $this->getNode('node')->setAttribute('is_defined_test', true);
             $compiler->subcompile($this->getNode('node'));
         }
         $compiler->raw(' ? ');
         $this->compileFilter($compiler, $filter);
         $compiler->raw(' : ');
         $compiler->subcompile($this->getNode('arguments')->getNode(0));
         $compiler->raw(')');
     } else {
         $this->compileFilter($compiler, $filter);
     }
 }
コード例 #22
0
ファイル: Include.php プロジェクト: 3dw1np/wf3
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     if ($this->getAttribute('ignore_missing')) {
         $compiler->write("try {\n")->indent();
     }
     if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
         $compiler->write("\$this->env->loadTemplate(")->subcompile($this->getNode('expr'))->raw(")->display(");
     } else {
         $compiler->write("\$template = \$this->env->resolveTemplate(")->subcompile($this->getNode('expr'))->raw(");\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");
     if ($this->getAttribute('ignore_missing')) {
         $compiler->outdent()->write("} catch (Twig_Error_Loader \$e) {\n")->indent()->write("// ignore missing template\n")->outdent()->write("}\n\n");
     }
 }
コード例 #23
0
ファイル: Test.php プロジェクト: ceroberoz/kurs
 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());
     }
     $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(')');
 }
コード例 #24
0
ファイル: For.php プロジェクト: faridos/ServerGroveLiveChat
 /**
  * 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")->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");
     }
     $compiler->write("foreach (\$context['_seq'] as ")->subcompile($this->getNode('key_target'))->raw(" => ")->subcompile($this->getNode('value_target'))->raw(") {\n")->indent();
     $compiler->subcompile($this->getNode('body'));
     if (null !== $this->getNode('else')) {
         $compiler->write("\$context['_iterated'] = true;\n");
     }
     if ($this->getAttribute('with_loop')) {
         $compiler->write("++\$context['loop']['index0'];\n")->write("++\$context['loop']['index'];\n")->write("\$context['loop']['first'] = false;\n")->write("if (isset(\$context['loop']['length'])) {\n")->indent()->write("--\$context['loop']['revindex0'];\n")->write("--\$context['loop']['revindex'];\n")->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")->outdent()->write("}\n");
     }
     $compiler->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_merge(\$_parent, array_intersect_key(\$context, \$_parent));\n");
 }
コード例 #25
0
ファイル: GetAttr.php プロジェクト: sweetroll/craft-test
 public function compile(Twig_Compiler $compiler)
 {
     if (function_exists('twig_template_get_attributes') && !$this->getAttribute('disable_c_ext')) {
         $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'));
     // only generate optional arguments when needed (to make generated code more readable)
     $needFourth = $this->getAttribute('ignore_strict_check');
     $needThird = $needFourth || $this->getAttribute('is_defined_test');
     $needSecond = $needThird || Twig_Template::ANY_CALL !== $this->getAttribute('type');
     $needFirst = $needSecond || null !== $this->getNode('arguments');
     if ($needFirst) {
         if (null !== $this->getNode('arguments')) {
             $compiler->raw(', ')->subcompile($this->getNode('arguments'));
         } else {
             $compiler->raw(', array()');
         }
     }
     if ($needSecond) {
         $compiler->raw(', ')->repr($this->getAttribute('type'));
     }
     if ($needThird) {
         $compiler->raw(', ')->repr($this->getAttribute('is_defined_test'));
     }
     if ($needFourth) {
         $compiler->raw(', ')->repr($this->getAttribute('ignore_strict_check'));
     }
     $compiler->raw(')');
 }
コード例 #26
0
ファイル: Constant.php プロジェクト: DeDoOozZz/brighterycms
 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('))');
 }
コード例 #27
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");
 }
コード例 #28
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(')');
 }
コード例 #29
0
ファイル: Element.php プロジェクト: luisfredgs/TwigView
 /**
  * Compile node.
  *
  * @param \Twig_Compiler $compiler Compiler.
  *
  * @return void
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->raw('echo $context[\'_view\']->element(');
     $compiler->subcompile($this->getNode('name'));
     $data = $this->getNode('data');
     if ($data !== null) {
         $compiler->raw(',');
         $compiler->subcompile($data);
     }
     $options = $this->getNode('options');
     if ($data !== null) {
         $compiler->raw(',');
         $compiler->subcompile($options);
     }
     $compiler->raw(");\n");
 }
コード例 #30
0
ファイル: MenuNode.php プロジェクト: vjousse/MenuBundle
 /**
  * @param \Twig_Compiler $compiler
  * @return void
  */
 public function compile($compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write("echo \$this->env->getExtension('menu')->get(")->subcompile($this->getNode('value'))->raw(")->render(");
     if (null !== $this->getNode('depth')) {
         $compiler->subcompile($this->getNode('depth'));
     }
     $compiler->raw(");\n");
 }