Is it possible to output the template name being used? Using a global variable or something similar? I am wanting to check which template is being used in a conditional.
Asked
Active
Viewed 5,432 times
4
-
I don't think that's possible, but can you give an example why you want to check the template-name? – Victor Jun 24 '14 at 21:32
4 Answers
15
This applies to Craft 2. For Craft 3+, see hashchange's answer.
You can get the current template’s name like so:
{{ _self.getTemplateName() }}
Todd Prouty
- 270
- 1
- 7
Brandon Kelly
- 34,307
- 2
- 71
- 137
-
2Careful with this; it returns an object of type
Twig_Markup, which is then casted to a string. If you need template name strictly as a string, use_self.getTemplateName().__toString– Alex Apr 21 '17 at 15:09
10
In Craft 3, {{ _self.getTemplateName() }} throws an error, but {{ _self }} does the trick.
hashchange
- 201
- 2
- 4
5
I don't believe this is currently possible outside of the context of a Craft plugin. Inside of a plugin, you could probably do it through a Twig extension so that you have access to the template instance, where you could call $template->getTemplateName(), which is part of Twig's Twig_Template abstract class.
Brad Bell
- 67,440
- 6
- 73
- 143
3
Post marked as answer doesn't work for me nowadays. For me works:
{{ craft.app.requestedParams.template }}
Дмитрий Боровков
- 31
- 1