From: Fabien Potencier Date: Wed, 29 Dec 2010 09:17:06 +0000 (+0100) Subject: fixed scope for macros (macros can be imported in a block or outside of a block) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=ac7803f9fc1a2d5007f780c5319f1b6e3c6221cd;p=web%2Fkonrad%2Ftwig.git fixed scope for macros (macros can be imported in a block or outside of a block) {% import "macros" as foo %} {% from "macros" import foo, bar as bar %} {% block foo %} {% import "macros" as foofoo %} {% from "macros" import foo as foofoo, bar as barbar %} {{ foo('something') }} {{ bar('something') }} {{ foofoo('something') }} {{ barbar('something') }} {% endblock %} --- diff --git a/lib/Twig/Node/Expression/AssignLocalName.php b/lib/Twig/Node/Expression/AssignLocalName.php deleted file mode 100644 index 68868a8..0000000 --- a/lib/Twig/Node/Expression/AssignLocalName.php +++ /dev/null @@ -1,23 +0,0 @@ - - */ -class Twig_Node_Expression_AssignLocalName extends Twig_Node_Expression_LocalName -{ -} diff --git a/lib/Twig/Node/Expression/LocalName.php b/lib/Twig/Node/Expression/LocalName.php deleted file mode 100644 index aac10bb..0000000 --- a/lib/Twig/Node/Expression/LocalName.php +++ /dev/null @@ -1,40 +0,0 @@ - - */ -class Twig_Node_Expression_LocalName extends Twig_Node_Expression -{ - static protected $counter = 0; - - public function __construct($name = null, $lineno = null) - { - if (null === $name) { - $uniq = self::$counter++; - $name = '__'.$uniq; - } - - parent::__construct(array(), array('name' => $name), $lineno); - } - - public function compile($compiler) - { - $compiler->raw('$'.$this->getAttribute('name')); - } -} - diff --git a/lib/Twig/Node/From.php b/lib/Twig/Node/From.php index 0af0fea..224a905 100644 --- a/lib/Twig/Node/From.php +++ b/lib/Twig/Node/From.php @@ -19,6 +19,6 @@ class Twig_Node_From extends Twig_Node_Import { public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null) { - parent::__construct($expr, new Twig_Node_Expression_AssignLocalName(null, $lineno), $lineno, $tag); + parent::__construct($expr, new Twig_Node_Expression_AssignName(str_replace('.', '_', uniqid('_imported_', true)), $lineno), $lineno, $tag); } } diff --git a/lib/Twig/Parser.php b/lib/Twig/Parser.php index 0d13798..06830e2 100644 --- a/lib/Twig/Parser.php +++ b/lib/Twig/Parser.php @@ -195,18 +195,16 @@ class Twig_Parser implements Twig_ParserInterface public function addImportedFunction($alias, $name, Twig_Node_Expression $node) { - $this->importedFunctions[0][$alias] = array( - 'name' => $name, - 'node' => $node, - ); + $this->importedFunctions[0][$alias] = array('name' => $name, 'node' => $node); } public function getImportedFunction($alias) { - if (!isset($this->importedFunctions[0][$alias])) { - return null; + foreach ($this->importedFunctions as $functions) { + if (isset($functions[$alias])) { + return $functions[$alias]; + } } - return $this->importedFunctions[0][$alias]; } public function pushLocalScope() diff --git a/lib/Twig/TokenParser/Filter.php b/lib/Twig/TokenParser/Filter.php index b800f10..35d3b54 100644 --- a/lib/Twig/TokenParser/Filter.php +++ b/lib/Twig/TokenParser/Filter.php @@ -19,7 +19,7 @@ class Twig_TokenParser_Filter extends Twig_TokenParser */ public function parse(Twig_Token $token) { - $name = '_tmp'.rand(10000, 99999); + $name = str_replace('.', '_', uniqid('_tmp_', true)); $node = new Twig_Node_Expression_Name($name, $token->getLine()); $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($node, $this->getTag());