Skip to content

Commit

Permalink
reintroduced _self (returns the current template name)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 19, 2016
1 parent f99650a commit 1015a8f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions 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)
Expand Down
1 change: 1 addition & 0 deletions doc/templates.rst
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions lib/Twig/Node/Expression/Name.php
Expand Up @@ -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()',
);
Expand Down
8 changes: 8 additions & 0 deletions 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
2 changes: 2 additions & 0 deletions test/Twig/Tests/Node/Expression/NameTest.php
Expand Up @@ -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));
Expand All @@ -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"),
);
}
Expand Down

0 comments on commit 1015a8f

Please sign in to comment.