/**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     list($msg, $vars) = $this->compileString($this->nodes['body']);
     if (null !== $this->nodes['plural']) {
         list($msg1, $vars1) = $this->compileString($this->nodes['plural']);
         $vars = array_merge($vars, $vars1);
     }
     $function = null === $this->nodes['plural'] ? '__' : '__n';
     if ($vars) {
         $compiler->write('echo strtr(' . $function . '(')->subcompile($msg);
         if (null !== $this->nodes['plural']) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->nodes['count'])->raw(')');
         }
         $compiler->raw(', true), array(');
         // modified: cakephp $return flag
         foreach ($vars as $var) {
             if ('count' === $var->getAttribute('name')) {
                 $compiler->string('%count%')->raw(' => abs(')->subcompile($this->nodes['count'])->raw('), ');
             } else {
                 $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', ');
             }
         }
         $compiler->raw("));\n");
     } else {
         $compiler->write('echo ' . $function . '(')->subcompile($msg);
         if (null !== $this->nodes['plural']) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->nodes['count'])->raw(')');
         }
         $compiler->raw(', true);');
         // modified: cakephp $return flag
     }
 }
示例#2
0
文件: Macro.php 项目: naldz/cyberden
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write(sprintf('public function get%s(', $this->getAttribute('name')));
     $count = count($this->getNode('arguments'));
     $pos = 0;
     foreach ($this->getNode('arguments') as $name => $default) {
         $compiler->raw('$__' . $name . '__ = ')->subcompile($default);
         if (++$pos < $count) {
             $compiler->raw(', ');
         }
     }
     if (PHP_VERSION_ID >= 50600) {
         if ($count) {
             $compiler->raw(', ');
         }
         $compiler->raw('...$__varargs__');
     }
     $compiler->raw(")\n")->write("{\n")->indent();
     $compiler->write("\$context = \$this->env->mergeGlobals(array(\n")->indent();
     foreach ($this->getNode('arguments') as $name => $default) {
         $compiler->write('')->string($name)->raw(' => $__' . $name . '__')->raw(",\n");
     }
     $compiler->write('')->string(self::VARARGS_NAME)->raw(' => ');
     if (PHP_VERSION_ID >= 50600) {
         $compiler->raw("\$__varargs__,\n");
     } else {
         $compiler->raw('func_num_args() > ')->repr($count)->raw(' ? array_slice(func_get_args(), ')->repr($count)->raw(") : array(),\n");
     }
     $compiler->outdent()->write("));\n\n")->write("\$blocks = array();\n\n")->write("ob_start();\n")->write("try {\n")->indent()->subcompile($this->getNode('body'))->outdent()->write("} catch (Exception \$e) {\n")->indent()->write("ob_end_clean();\n\n")->write("throw \$e;\n")->outdent()->write("} catch (Throwable \$e) {\n")->indent()->write("ob_end_clean();\n\n")->write("throw \$e;\n")->outdent()->write("}\n\n")->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")->outdent()->write("}\n\n");
 }
示例#3
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     list($msg, $vars) = $this->compileString($this->getNode('body'));
     if (null !== $this->getNode('plural')) {
         list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
         $vars = array_merge($vars, $vars1);
     }
     $function = null === $this->getNode('plural') ? '\\Skeleton\\I18n\\Translation::translate' : '\\Skeleton\\I18n\\Translation::translate_plural';
     if ($vars) {
         $compiler->write('echo strtr(' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw(', $context[\'env\'][\'translation\']), array(');
         foreach ($vars as $var) {
             if ('count' === $var->getAttribute('name')) {
                 $compiler->string('%count%')->raw(' => abs(')->subcompile($this->getNode('count'))->raw('), ');
             } else {
                 $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', ');
             }
         }
         $compiler->raw("));\n");
     } else {
         $compiler->write('echo ' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw(", " . '$context[\'env\'][\'translation\']' . ");\n");
     }
 }
示例#4
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);
 }
示例#5
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(')');
 }
示例#6
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");
     }
 }
示例#7
0
文件: For.php 项目: n0way/Twig
 /**
  * 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();
     if (null !== $this->getNode('ifexpr')) {
         $compiler->write("if (")->subcompile($this->getNode('ifexpr'))->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");
     }
     if (null !== $this->getNode('ifexpr')) {
         $compiler->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");
 }
 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 . '\']');
     }
 }
示例#9
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     list($msg, $vars) = $this->compileString($this->getNode('body'));
     if (null !== $this->getNode('plural')) {
         list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
         $vars = array_merge($vars, $vars1);
     }
     $function = null === $this->getNode('plural') ? 'gettext' : 'ngettext';
     if ($vars) {
         $compiler->write('echo strtr(' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw('), array(');
         foreach ($vars as $var) {
             if ('count' === $var->getAttribute('name')) {
                 $compiler->string('%count%')->raw(' => abs(')->subcompile($this->getNode('count'))->raw('), ');
             } else {
                 $compiler->string('%' . $var->getAttribute('name') . '%')->raw(' => ')->subcompile($var)->raw(', ');
             }
         }
         $compiler->raw("));\n");
     } else {
         $compiler->write('echo ' . $function . '(')->subcompile($msg);
         if (null !== $this->getNode('plural')) {
             $compiler->raw(', ')->subcompile($msg1)->raw(', abs(')->subcompile($this->getNode('count'))->raw(')');
         }
         $compiler->raw(");\n");
     }
 }
示例#10
0
 protected function addGetTemplate(Twig_Compiler $compiler)
 {
     if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
         $compiler->write("\$this->env->loadTemplate(")->subcompile($this->getNode('expr'))->raw(")");
     } else {
         $compiler->write("\$template = \$this->env->resolveTemplate(")->subcompile($this->getNode('expr'))->raw(");\n")->write('$template');
     }
 }
示例#11
0
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     foreach ($this->_files as $file) {
         $compiler->write("// asset \"{$file}\"\n")->write('$context[')->repr($this->getAttribute('var_name'))->raw('] = ')->repr($this->getAssetUrl($file));
         $compiler->raw(";\n")->subcompile($this->getNode('body'));
         $compiler->write('unset($context[')->repr($this->getAttribute('var_name'))->raw("]);\n");
     }
 }
示例#12
0
 /**
  * Compiles a Exit_Node into PHP.
  *
  * @param \Twig_Compiler $compiler
  *
  * @return null
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     if ($status = $this->getNode('status')) {
         $compiler->write('throw new \\Craft\\HttpException(')->subcompile($status)->raw(");\n");
     } else {
         $compiler->write("\\Craft\\craft()->end();\n");
     }
 }
 /** {@inheritdoc} */
 public function compile(\Twig_Compiler $compiler)
 {
     if (false !== ($file = $this->getAttribute('js_file'))) {
         $compiler->write('echo ')->string('<script type="text/javascript" src="' . $file . '"></script>')->raw(";\n");
     }
     if (false !== ($file = $this->getAttribute('css_file'))) {
         $compiler->write('echo ')->string('<link rel="stylesheet" href="' . $file . '">')->raw(";\n");
     }
 }
示例#14
0
 /**
  * Compiles the tag
  *
  * @param object $compiler 
  * @return void
  * @author Jonathan Geiger
  */
 public function compile(Twig_Compiler $compiler)
 {
     if ($this->getNode('params')) {
         $compiler->write('$route_params = ')->subcompile($this->getNode('params'))->raw(";\n");
     } else {
         $compiler->write('$route_params = array()')->raw(";\n");
     }
     // Output the route
     $compiler->write('echo Kohana::$base_url.Route::get(')->subcompile($this->getNode('route'))->write(')->uri($route_params)')->raw(";\n");
 }
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $content = "\$this->env->getExtension('content')";
     $name = $this->getAttribute('name');
     // Inject the placeholder as the content if the content is empty
     $compiler->write("if(!{$content}->has('{$name}'))" . PHP_EOL)->write("{" . PHP_EOL)->indent()->write("ob_start();" . PHP_EOL)->subcompile($this->getNode('placeholder'))->write("{$content}->add('{$name}', ob_get_contents());" . PHP_EOL)->write("ob_end_clean();" . PHP_EOL)->outdent()->write("}" . PHP_EOL);
     // Output the content
     $compiler->write("echo \$this->env->getExtension('content')->render('{$name}');" . PHP_EOL);
 }
示例#16
0
 /**
  * {@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("));");
 }
示例#17
0
 public function compile(\Twig_Compiler $compiler)
 {
     foreach ($this->getAttribute('above_dumps') as $aboveDump) {
         $compiler->subcompile($aboveDump);
     }
     $compiler->addDebugInfo($this)->write("ob_start();\n")->subcompile($this->getAttribute('below_dump'));
     $compiler->write("\$buffer = ob_get_clean();\n");
     // echo $this->env->getExtension('stamp')->dumpStamp('svg');
     $compiler->addDebugInfo($this)->write("echo \$this->env->getExtension('stamp')->dumpStamp(")->string($this->getAttribute('name'))->raw(");\n");
     $compiler->write("echo \$buffer;\n");
 }
示例#18
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");
 }
示例#19
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     if ($this->getAttribute('capture')) {
         $compiler->write("ob_start();\n")->subcompile($this->getNode('value'));
         $compiler->write("\$value = ('' === \$value = ob_get_clean()) ? '' : new \\Twig_Markup(\$value, \$this->env->getCharset());\n");
     } else {
         $compiler->write("\$value = ")->subcompile($this->getNode('value'))->raw(";\n");
     }
     $compiler->write("\$context['definition']->set('")->raw($this->getNode('name')->getAttribute('name'))->raw("', \$value);\n");
 }
示例#20
0
文件: kxEnv.php 项目: D4rk4/Kusaba-z
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $val = $this->getNode('expr')->getAttribute('value');
     if ($val == 'DUMP_CONFIG') {
         // For debugging
         $compiler->write("echo kxEnv::dumpConfig();\n");
     } else {
         $compiler->write("echo kxEnv::Get('kx:{$val}');\n");
     }
 }
示例#21
0
 /**
  * Compiles the node into PHP code for execution by Twig
  *
  * @param \Twig_Compiler $compiler The compiler to which we add the node's PHP code
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this);
     $compiler->write('$highlighter = \\Ramsey\\Twig\\CodeBlock\\Highlighter\\HighlighterFactory::getHighlighter(')->string($this->getHighlighterName())->raw(', ')->repr($this->getHighlighterArgs())->raw(");\n");
     $compiler->write('$highlightedCode = $highlighter->highlight(')->string($this->getNode('body')->getAttribute('data'))->raw(', ')->repr($this->attributes)->raw(");\n");
     if ($this->hasAttribute('format') && $this->getAttribute('format') == 'html') {
         $compiler->write('$figcaption = ')->string($this->getFigcaption())->raw(";\n")->write('echo sprintf(')->raw('"<figure class=\\"code-highlight-figure\\">%s%s</figure>\\n",')->raw(' $figcaption, $highlightedCode')->raw(");\n");
     } else {
         $compiler->write('echo $highlightedCode;' . "\n");
     }
 }
示例#22
0
 /**
  * @param Twig_Compiler $compiler
  *
  * @return void
  * @author Jonathan Geiger
  */
 public function compile(Twig_Compiler $compiler)
 {
     $compiler->write('if (!fragment::load(')->subcompile($this->getNode('key'));
     // Lifetime will be false if it wasn't parsed
     if ($this->lifetime) {
         $compiler->write(', ')->subcompile($this->getNode('lifetime'))->write(')) {');
     } else {
         $compiler->write(')) {');
     }
     $compiler->raw("\n")->subcompile($this->getNode('data'))->raw("\n")->write('fragment::save();')->raw("\n}\n");
 }
示例#23
0
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write('ob_start();' . PHP_EOL)->subcompile($this->getNode('body'))->write('$source = ob_get_clean();' . PHP_EOL)->write('$geshi = new GeSHi($source, \'' . $this->getAttribute('language') . '\');' . PHP_EOL);
     if ($this->getAttribute('use_classes')) {
         $compiler->write('$geshi->enable_classes();' . PHP_EOL);
     }
     if ($this->getAttribute('line_numbers')) {
         $compiler->write('$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);' . PHP_EOL);
     }
     $compiler->write('echo $geshi->parse_code();' . PHP_EOL);
 }
示例#24
0
 /**
  * Собирает 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");
 }
 /**
  * @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");
 }
示例#26
0
 private function compileTemplateCall(Twig_Compiler $compiler, $method)
 {
     if (!$this->hasNode('template')) {
         $compiler->write('$this');
     } else {
         $compiler->write('$this->loadTemplate(')->subcompile($this->getNode('template'))->raw(', ')->repr($this->getTemplateName())->raw(', ')->repr($this->getTemplateLine())->raw(')');
     }
     $compiler->raw(sprintf('->%s', $method));
     $this->compileBlockArguments($compiler);
     return $compiler;
 }
示例#27
0
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler $compiler
  *
  * @return null
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $compiler->addDebugInfo($this)->write("switch (")->subcompile($this->getNode('value'))->raw(") {\n")->indent();
     foreach ($this->_cases as $case) {
         $compiler->write('case ')->subcompile($case['expr'])->raw(":\n")->write("{\n")->indent()->subcompile($case['body'])->write("break;\n")->outdent()->write("}\n");
     }
     if ($this->hasNode('default') && $this->getNode('default') !== null) {
         $compiler->write("default:\n")->write("{\n")->indent()->subcompile($this->getNode('default'))->outdent()->write("}\n");
     }
     $compiler->outdent()->write("}\n");
 }
示例#28
0
 private function compileMaxPerPage(\Twig_Compiler $compiler)
 {
     $compiler->write("public function getMaxesPerPage()\n", "{\n")->indent();
     $compiler->write("return array(\n");
     $compiler->indent();
     foreach ($this->getAttribute('maxesPerPage') as $maxPerPage) {
         $compiler->write($maxPerPage . ",\n");
     }
     $compiler->outdent();
     $compiler->write(");\n");
     $compiler->outdent()->write("}\n\n");
 }
示例#29
0
 /**
  * Compiles the node to PHP.
  *
  * @param Twig_Compiler $compiler A Twig_Compiler instance
  */
 public function compile(Twig_Compiler $compiler)
 {
     if ($this->getAttribute('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");
         if (!$this->getAttribute('ifexpr')) {
             $compiler->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");
         }
     }
 }
示例#30
0
文件: Content.php 项目: jarves/jarves
 /**
  * Compiles the node to PHP.
  *
  * @param \Twig_Compiler A Twig_Compiler instance
  */
 public function compile(\Twig_Compiler $compiler)
 {
     $methodName = 'content' === $this->name ? 'renderSingleSlot' : 'renderSlot';
     $compiler->addDebugInfo($this)->write("\$renderParams = [];\n");
     if (null !== ($type = $this->getNode('type'))) {
         $compiler->write("\$renderParams['type'] = ")->subcompile($type)->write(";\n");
     }
     if (null !== ($options = $this->getNode('options'))) {
         $compiler->write("\$renderParams['options'] = ")->subcompile($options)->write(";\n");
     }
     $compiler->write("echo \$context['jarves_content_render']->{$methodName}(null")->write(', ')->subcompile($this->getNode('id'))->write(', $renderParams')->write(");\n");
 }