Changes:
+ * added "needs_context" option for filters and functions (the context is then passed as a first argument)
* added global variables support
* made macros return their value instead of echoing directly (fixes calling a macro in sandbox mode)
* added the "from" tag to import macros as functions
{
$this->options = array_merge(array(
'needs_environment' => false,
+ 'needs_context' => false,
'pre_escape' => null,
), $options);
}
return $this->options['needs_environment'];
}
+ public function needsContext()
+ {
+ return $this->options['needs_context'];
+ }
+
public function getSafe(Twig_Node $filterArgs)
{
if (isset($this->options['is_safe'])) {
{
$this->options = array_merge(array(
'needs_environment' => false,
+ 'needs_context' => false,
), $options);
}
return $this->options['needs_environment'];
}
+ public function needsContext()
+ {
+ return $this->options['needs_context'];
+ }
+
public function getSafe(Twig_Node $functionArgs)
{
if (isset($this->options['is_safe'])) {
protected function compileFilter(Twig_Compiler $compiler, Twig_FilterInterface $filter)
{
- $compiler->raw($filter->compile().($filter->needsEnvironment() ? '($this->env, ' : '('));
+ $compiler
+ ->raw($filter->compile().'(')
+ ->raw($filter->needsEnvironment() ? '$this->env, ' : '')
+ ->raw($filter->needsContext() ? '$context, ' : '')
+ ;
$this->getNode('node')->compile($compiler);
throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getNode('name')->getAttribute('name')), $this->getLine());
}
- $compiler->raw($function->compile().($function->needsEnvironment() ? '($this->env, ' : '('));
+ $compiler
+ ->raw($function->compile().'(')
+ ->raw($function->needsEnvironment() ? '$this->env, ' : '')
+ ->raw($function->needsContext() ? '$context, ' : '')
+ ;
$first = true;
foreach ($this->getNode('arguments') as $node) {