From 508a2411799ef9a7fca928aa8edac322b4f115b9 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 19 Dec 2010 22:24:10 +0100 Subject: [PATCH] forced functions to have fn_ as a prefix to avoid name clashes between functions and variables --- doc/advanced.rst | 10 ++++++++-- lib/Twig/Node/Expression/Function.php | 3 +++ lib/Twig/Node/From.php | 2 +- test/Twig/Tests/Fixtures/expressions/function.test | 4 ++-- test/Twig/Tests/Fixtures/globals.test | 4 ++-- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/doc/advanced.rst b/doc/advanced.rst index 97a5618..9634b1b 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -115,11 +115,14 @@ A function is a variable that is callable. The value is an instance of ``Twig_Function``:: $twig = new Twig_Environment($loader); - $twig->addGlobal('lipsum', new Twig_Function(new Text(), 'getLipsum')); + $twig->addGlobal('fn_lipsum', new Twig_Function(new Text(), 'getLipsum')); // or $twig->addGlobal('text', new Text()); - $twig->addGlobal('lipsum', new Twig_Function('text', 'getLipsum')); + $twig->addGlobal('fn_lipsum', new Twig_Function('text', 'getLipsum')); + +To avoid name clashes with variables, function names must be prefixed with +``fn_`` when defined (the prefix must not be added in templates). The first argument to ``Twig_Function`` is an object or a variable name referencing an object. @@ -128,6 +131,9 @@ You can then use the ``lipsum`` function anywhere in a template: .. code-block:: jinja + {# A lipsum variable does not override the lipsum function #} + {% set lipsum = 'foo' %} + {{ lipsum(40) }} .. note:: diff --git a/lib/Twig/Node/Expression/Function.php b/lib/Twig/Node/Expression/Function.php index 0cf5e95..6b5c131 100644 --- a/lib/Twig/Node/Expression/Function.php +++ b/lib/Twig/Node/Expression/Function.php @@ -17,6 +17,9 @@ class Twig_Node_Expression_Function extends Twig_Node_Expression public function compile($compiler) { + // functions must be prefixed with fn_ + $this->getNode('name')->setAttribute('name', 'fn_'.$this->getNode('name')->getAttribute('name')); + $compiler ->raw('$this->callFunction($context, ') ->subcompile($this->getNode('name')) diff --git a/lib/Twig/Node/From.php b/lib/Twig/Node/From.php index a71a7e2..f75392a 100644 --- a/lib/Twig/Node/From.php +++ b/lib/Twig/Node/From.php @@ -36,7 +36,7 @@ class Twig_Node_From extends Twig_Node_Import foreach ($this->getAttribute('imports') as $name => $alias) { $compiler ->write('$context[') - ->repr($alias) + ->repr('fn_'.$alias) ->raw('] = new Twig_Function(') ->subcompile($this->getNode('var')) ->raw(', ') diff --git a/test/Twig/Tests/Fixtures/expressions/function.test b/test/Twig/Tests/Fixtures/expressions/function.test index 90409ad..4175f9c 100644 --- a/test/Twig/Tests/Fixtures/expressions/function.test +++ b/test/Twig/Tests/Fixtures/expressions/function.test @@ -6,8 +6,8 @@ Twig supports calling variables --DATA-- return array( 'foo' => new Foo(), - 'lower' => new Twig_Function('foo', 'strToLower'), - 'lower1' => new Twig_Function(new Foo(), 'strToLower')) + 'fn_lower' => new Twig_Function('foo', 'strToLower'), + 'fn_lower1' => new Twig_Function(new Foo(), 'strToLower')) --EXPECT-- foo foo diff --git a/test/Twig/Tests/Fixtures/globals.test b/test/Twig/Tests/Fixtures/globals.test index 02d577c..e816ca6 100644 --- a/test/Twig/Tests/Fixtures/globals.test +++ b/test/Twig/Tests/Fixtures/globals.test @@ -14,8 +14,8 @@ global variables {{ foo() }} --DATA-- -$twig->addGlobal('foo', new Twig_Function(new Foo(), 'getFoo')); -$twig->addGlobal('bar', new Twig_Function('barObj', 'getFoo')); +$twig->addGlobal('fn_foo', new Twig_Function(new Foo(), 'getFoo')); +$twig->addGlobal('fn_bar', new Twig_Function('barObj', 'getFoo')); return array('barObj' => new Foo()); --EXPECT-- foo -- 1.7.2.5