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(')'); }
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(')'); }
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); } }
protected function compileClassHeader(Twig_Compiler $compiler) { $compiler->write("<?php\n\n")->write("/* " . str_replace('*/', '* /', $this->getAttribute('filename')) . " */\n")->write('class ' . $compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename')))->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))->write("{\n")->indent(); if (null !== $this->getNode('parent')) { $compiler->write("protected \$parent;\n\n"); } }
public function compile(Twig_Compiler $compiler) { $name = $this->getNode('filter')->getAttribute('value'); if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) { $message = sprintf('The filter "%s" does not exist', $name); if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFilters()))) { $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); } throw new Twig_Error_Syntax($message, $this->getLine()); } $this->compileFilter($compiler, $filter); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); 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 { if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : null)'); } else { // When Twig will require PHP 7.0, the Template::notFound() method // will be removed and the code inlined like this: // (function () { throw new Exception(...); })(); $compiler->raw('(isset($context[')->string($name)->raw(']) || array_key_exists(')->string($name)->raw(', $context) ? $context[')->string($name)->raw('] : $this->notFound(')->string($name)->raw(', ')->repr($this->lineno)->raw('))'); } } }
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(')'); }
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(')'); }
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', '>=')) { // PHP 5.4 ternary operator performance was optimized $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } else { $compiler->raw('$this->getContext($context, ')->string($name); if ($this->getAttribute('ignore_strict_check')) { $compiler->raw(', true'); } $compiler->raw(')'); } } }
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(')'); }
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()); } // defined is a special case if ('defined' === $this->getAttribute('name')) { if ($this->getNode('node') instanceof Twig_Node_Expression_Name) { $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->raw('(null !== ')->subcompile($this->getNode('node'))->raw(')'); } else { throw new Twig_Error_Syntax('The "defined" test only works with simple variables', $this->getLine()); } return; } $compiler->raw($testMap[$this->getAttribute('name')]->compile() . '(')->subcompile($this->getNode('node')); if (null !== $this->getNode('arguments')) { $compiler->raw(', '); $max = count($this->getNode('arguments')) - 1; foreach ($this->getNode('arguments') as $i => $node) { $compiler->subcompile($node); if ($i != $max) { $compiler->raw(', '); } } } $compiler->raw(')'); }
public function compile(Twig_Compiler $compiler) { $name = $this->getNode('filter')->getAttribute('value'); if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) { throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine()); } $this->compileFilter($compiler, $filter); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $test = $compiler->getEnvironment()->getTest($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'test'); $this->setAttribute('callable', $test->getCallable()); $this->compileCallable($compiler); }
/** * Compile the node. * * @param \Twig_Compiler $compiler A Twig compiler instance. */ public function compile(\Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $shortcode = $compiler->getEnvironment()->getShortcode($name); $filter = $compiler->getEnvironment()->getShortcodeFilter($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'shortcode'); $this->setAttribute('thing', $shortcode); $this->setAttribute('needs_environment', $shortcode->needsEnvironment()); $this->setAttribute('needs_context', $shortcode->needsContext()); $this->setAttribute('arguments', $shortcode->getArguments()); if ($shortcode instanceof \Twig_FunctionCallableInterface || $shortcode instanceof GenericShortcode) { $instance = ShortcodesTrait::getShortcodesClass(); $this->setAttribute('callable', $shortcode->getCallable()); } if ($this->hasNode('node')) { $body = $this->getNode('node'); if (!is_array($body)) { $body = [$body]; } $compiler->addDebugInfo($this)->write("ob_start();\n"); foreach ($body as $key => $node) { $compiler->subcompile($node); } $compiler->write("\$body = ob_get_clean();\n"); } if ($this->hasNode('arguments') && null !== $this->getNode('arguments')) { $compiler->write("\$arguments = array("); foreach ($this->getNode('arguments') as $key => $node) { $compiler->string($key)->raw(" => ")->subcompile($node)->raw(", "); } $compiler->raw(");\n"); } $compiler->write('$compiled = $context["__shortcodes"](')->string($this->tag)->raw(", \$body, \$arguments);\n")->write($filter ? '$compiled = ' : 'echo '); $this->compileCallable($compiler); $compiler->raw(";\n"); // Filter shortcode, if registered filters are present if ($filter) { $compiler->write('echo $context["__shortcodes_filter"](')->string($name)->raw(', $compiled, $context, $this->env)'); } $compiler->raw(";\n")->write('unset($body, $arguments, $compiled);')->raw("\n"); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $test = $compiler->getEnvironment()->getTest($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'Test'); $this->setAttribute('thing', $test); if ($test instanceof Twig_TestCallableInterface || $test instanceof Twig_SimpleTest) { $this->setAttribute('callable', $test->getCallable()); } $this->compileCallable($compiler); }
public function compile(Twig_Compiler $compiler) { $name = $this->getNode('filter')->getAttribute('value'); $filter = $compiler->getEnvironment()->getFilter($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'filter'); $this->setAttribute('needs_environment', $filter->needsEnvironment()); $this->setAttribute('needs_context', $filter->needsContext()); $this->setAttribute('arguments', $filter->getArguments()); $this->setAttribute('callable', $filter->getCallable()); $this->compileCallable($compiler); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); 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 { if (PHP_VERSION_ID >= 70000) { // use PHP 7 null coalescing operator $compiler->raw('($context[')->string($name)->raw('] ?? '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } elseif (PHP_VERSION_ID >= 50400) { // PHP 5.4 ternary operator performance was optimized $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } else { $compiler->raw('$this->getContext($context, ')->string($name); if ($this->getAttribute('ignore_strict_check')) { $compiler->raw(', true'); } $compiler->raw(')'); } } }
/** * {@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"); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $function = $compiler->getEnvironment()->getFunction($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'function'); $this->setAttribute('needs_environment', $function->needsEnvironment()); $this->setAttribute('needs_context', $function->needsContext()); $this->setAttribute('arguments', $function->getArguments()); $this->setAttribute('callable', $function->getCallable()); $this->setAttribute('is_variadic', $function->isVariadic()); $this->compileCallable($compiler); }
public function compile(Twig_Compiler $compiler) { if ($compiler->getEnvironment()->isStrictVariables()) { $name = $this->getAttribute('name'); $type = $this->getAttribute('type'); $compiler->addDebugInfo($this); if (0 === strcasecmp($type, 'array')) { $compiler->write("if (false === is_array(\$context['{$name}'])) {\n"); } else { $compiler->write("if (false === \$context['{$name}'] instanceof {$type}) {\n"); } $compiler->indent()->write("throw new Twig_Error_Runtime('variable \\'{$name}\\' is expected to be of type {$type} but '.get_class(\$context['{$name}']).' was provided.', {$this->getLine()}, '{$compiler->getFilename()}');\n")->outdent()->write("}\n"); } }
public function compile(Twig_Compiler $compiler) { if ('_self' === $this->getAttribute('name')) { $compiler->raw('$this'); } elseif ('_context' === $this->getAttribute('name')) { $compiler->raw('$context'); } elseif ('_charset' === $this->getAttribute('name')) { $compiler->raw('$this->env->getCharset()'); } elseif ($compiler->getEnvironment()->isStrictVariables()) { $compiler->raw(sprintf('$this->getContext($context, \'%s\', \'%s\')', $this->getAttribute('name'), $this->lineno)); } else { $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $this->getAttribute('name'), $this->getAttribute('name'))); } }
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(')'); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $function = $compiler->getEnvironment()->getFunction($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'function'); $this->setAttribute('thing', $function); $this->setAttribute('needs_environment', $function->needsEnvironment()); $this->setAttribute('needs_context', $function->needsContext()); $this->setAttribute('arguments', $function->getArguments()); if ($function instanceof Twig_FunctionCallableInterface || $function instanceof Twig_SimpleFunction) { $this->setAttribute('callable', $function->getCallable()); } $this->compileCallable($compiler); }
public function compile(Twig_Compiler $compiler) { static $specialVars = array('_self' => '$this', '_context' => '$context', '_charset' => '$this->env->getCharset()'); $name = $this->getAttribute('name'); if ($this->hasAttribute('is_defined_test')) { if (isset($specialVars[$name])) { $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif (isset($specialVars[$name])) { $compiler->raw($specialVars[$name]); } elseif ($compiler->getEnvironment()->isStrictVariables()) { $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name)); } else { $compiler->raw(sprintf('(isset($context[\'%s\']) ? $context[\'%s\'] : null)', $name, $name)); } }
public function compile(Twig_Compiler $compiler) { $name = $this->getNode('filter')->getAttribute('value'); $filter = $compiler->getEnvironment()->getFilter($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'filter'); $this->setAttribute('thing', $filter); $this->setAttribute('needs_environment', $filter->needsEnvironment()); $this->setAttribute('needs_context', $filter->needsContext()); $this->setAttribute('arguments', $filter->getArguments()); if ($filter instanceof Twig_FilterCallableInterface || $filter instanceof Twig_SimpleFilter) { $this->setAttribute('callable', $filter->getCallable()); } if ($filter instanceof Twig_SimpleFilter) { $this->setAttribute('is_variadic', $filter->isVariadic()); } $this->compileCallable($compiler); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $testMap = $compiler->getEnvironment()->getTests(); $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(')'); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); if ($this->getAttribute('is_defined_test')) { if ($this->isSpecial()) { if ('_self' === $name) { @trigger_error(sprintf('Global variable "_self" is deprecated in %s at line %d', '?', $this->getLine()), E_USER_DEPRECATED); } $compiler->repr(true); } else { $compiler->raw('array_key_exists(')->repr($name)->raw(', $context)'); } } elseif ($this->isSpecial()) { if ('_self' === $name) { @trigger_error(sprintf('Global variable "_self" is deprecated in %s at line %d', '?', $this->getLine()), E_USER_DEPRECATED); } $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 (PHP_VERSION_ID >= 50400) { // PHP 5.4 ternary operator performance was optimized $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } else { $compiler->raw('$this->getContext($context, ')->string($name); if ($this->getAttribute('ignore_strict_check')) { $compiler->raw(', true'); } $compiler->raw(')'); } } }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $function = $compiler->getEnvironment()->getFunction($name); $this->setAttribute('name', $name); $this->setAttribute('type', 'function'); $this->setAttribute('thing', $function); $this->setAttribute('needs_environment', $function->needsEnvironment()); $this->setAttribute('needs_context', $function->needsContext()); $this->setAttribute('arguments', $function->getArguments()); if ($function instanceof Twig_FunctionCallableInterface || $function instanceof Twig_SimpleFunction) { $callable = $function->getCallable(); if ('constant' === $name && $this->getAttribute('is_defined_test')) { $callable = 'twig_constant_is_defined'; } $this->setAttribute('callable', $callable); } if ($function instanceof Twig_SimpleFunction) { $this->setAttribute('is_variadic', $function->isVariadic()); } $this->compileCallable($compiler); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $compiler->addDebugInfo($this); 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 { $compiler->raw('(isset($context[')->string($name)->raw(']) ? $context[')->string($name)->raw('] : '); if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) { $compiler->raw('null)'); } else { $compiler->raw('$this->getContext($context, ')->string($name)->raw('))'); } } }
public function compile(Twig_Compiler $compiler) { $name = $this->getNode('filter')->getAttribute('value'); if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) { throw new Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $this->getLine()); } $node = $this->getNode('node'); // 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 && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) { $compiler->raw('((')->subcompile(new Twig_Node_Expression_Test($node, 'defined', new Twig_Node(), $this->getLine()))->raw(') ? ('); $this->compileFilter($compiler, $filter); $compiler->raw(') : ('); if ($this->getNode('arguments')->hasNode(0)) { $compiler->subcompile($this->getNode('arguments')->getNode(0)); } else { $compiler->string(''); } $compiler->raw('))'); } else { $this->compileFilter($compiler, $filter); } }