From: Fabien Potencier Date: Wed, 5 Jan 2011 19:15:19 +0000 (+0100) Subject: added context support for functions and filters X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=957a30af759c0210e8173d0d340b3bcb9163f26d;p=web%2Fkonrad%2Ftwig.git added context support for functions and filters --- diff --git a/CHANGELOG b/CHANGELOG index 5f76db1..0291c6a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -14,6 +14,7 @@ Backward incompatibilities: 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 diff --git a/lib/Twig/Filter.php b/lib/Twig/Filter.php index 2e1ddcb..3484df2 100644 --- a/lib/Twig/Filter.php +++ b/lib/Twig/Filter.php @@ -23,6 +23,7 @@ abstract class Twig_Filter implements Twig_FilterInterface { $this->options = array_merge(array( 'needs_environment' => false, + 'needs_context' => false, 'pre_escape' => null, ), $options); } @@ -32,6 +33,11 @@ abstract class Twig_Filter implements Twig_FilterInterface 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'])) { diff --git a/lib/Twig/Function.php b/lib/Twig/Function.php index 9fcb655..99e9b77 100644 --- a/lib/Twig/Function.php +++ b/lib/Twig/Function.php @@ -23,6 +23,7 @@ abstract class Twig_Function implements Twig_FunctionInterface { $this->options = array_merge(array( 'needs_environment' => false, + 'needs_context' => false, ), $options); } @@ -31,6 +32,11 @@ abstract class Twig_Function implements Twig_FunctionInterface 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'])) { diff --git a/lib/Twig/Node/Expression/Filter.php b/lib/Twig/Node/Expression/Filter.php index cf5ffb3..d39c364 100644 --- a/lib/Twig/Node/Expression/Filter.php +++ b/lib/Twig/Node/Expression/Filter.php @@ -54,7 +54,11 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression 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); diff --git a/lib/Twig/Node/Expression/Function.php b/lib/Twig/Node/Expression/Function.php index 23911bc..92ba789 100644 --- a/lib/Twig/Node/Expression/Function.php +++ b/lib/Twig/Node/Expression/Function.php @@ -22,7 +22,11 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression 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) {