本文整理汇总了PHP中Twig_Compiler::write方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Compiler::write方法的具体用法?PHP Twig_Compiler::write怎么用?PHP Twig_Compiler::write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Compiler
的用法示例。
在下文中一共展示了Twig_Compiler::write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: compile
/**
* 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: compile
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: compile
/**
* 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: compile
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: compileTemplateCall
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: compile
/**
* 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: compile
/**
* 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");
}
示例8: compile
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: compile
/**
* 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: addGetTemplate
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: compile
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: compile
/**
* 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");
}
}
示例13: compile
/** {@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: compile
/**
* 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");
}
示例15: compile
/**
* 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);
}