From 1015a8f9dba3d5e72d9100b285e77d60defac9dd Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 16 Jan 2016 06:39:39 +0100 Subject: [PATCH] reintroduced _self (returns the current template name) --- CHANGELOG | 4 ++-- doc/templates.rst | 1 + lib/Twig/Node/Expression/Name.php | 1 + test/Twig/Tests/Fixtures/expressions/_self.test | 8 ++++++++ test/Twig/Tests/Node/Expression/NameTest.php | 2 ++ 5 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/expressions/_self.test diff --git a/CHANGELOG b/CHANGELOG index f8b2ea0857..c36d7a84eb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,9 +1,9 @@ -* 2.0.0 (201X-XX-XX) +* 2.0.0 (2016-XX-XX) * sped up the json_encode filter * removed reserved macro names; all names can be used as macro * removed Twig_Template::getEnvironment() - * removed _self variable (except for usage in from and import tags) + * changed _self variable to return the current template name * made the loader a required argument of Twig_Environment constructor * removed Twig_Environment::clearTemplateCache() * removed Twig_Autoloader (use Composer instead) diff --git a/doc/templates.rst b/doc/templates.rst index 0542c573a4..68438b2093 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -127,6 +127,7 @@ Global Variables The following variables are always available in templates: +* ``_self``: references the current template name; * ``_context``: references the current context; * ``_charset``: references the current charset. diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index fa2a0e92d2..89367b463f 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -12,6 +12,7 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression { private $specialVars = array( + '_self' => '$this->getTemplateName()', '_context' => '$context', '_charset' => '$this->env->getCharset()', ); diff --git a/test/Twig/Tests/Fixtures/expressions/_self.test b/test/Twig/Tests/Fixtures/expressions/_self.test new file mode 100644 index 0000000000..c116f8d7a8 --- /dev/null +++ b/test/Twig/Tests/Fixtures/expressions/_self.test @@ -0,0 +1,8 @@ +--TEST-- +_self returns the current template name +--TEMPLATE-- +{{ _self }} +--DATA-- +return array() +--EXPECT-- +index.twig diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index 10f51a1caa..f48f354279 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -21,6 +21,7 @@ public function testConstructor() public function getTests() { $node = new Twig_Node_Expression_Name('foo', 1); + $self = new Twig_Node_Expression_Name('_self', 1); $context = new Twig_Node_Expression_Name('_context', 1); $env = new Twig_Environment($this->getMock('Twig_LoaderInterface'), array('strict_variables' => true)); @@ -29,6 +30,7 @@ public function getTests() return array( array($node, "// line 1\n".'(isset($context["foo"]) || array_key_exists("foo", $context) ? $context["foo"] : $this->notFound("foo", 1))', $env), array($node, $this->getVariableGetter('foo', 1), $env1), + array($self, "// line 1\n\$this->getTemplateName()"), array($context, "// line 1\n\$context"), ); }