4

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.

1
  • I don't think that's possible, but can you give an example why you want to check the template-name?
    – Victor
    Commented Jun 24, 2014 at 21:32

4 Answers 4

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() }}
1
  • 2
    Careful 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
    Commented Apr 21, 2017 at 15:09
10

In Craft 3, {{ _self.getTemplateName() }} throws an error, but {{ _self }} does the trick.

6

Post marked as answer doesn't work for me nowadays. For me works:

{{ craft.app.requestedParams.template }}
2
  • Thanks, works for me too (Craft 4).
    – GazCrane
    Commented Nov 25, 2022 at 10:10
  • Thanks, works in CraftCMS 5 as well. Commented May 26 at 18:54
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.

2
  • 1
    Thanks Brad, would you consider adding a tag for it a some point?
    – Gareth
    Commented Jun 25, 2014 at 5:57
  • I don't see why not!
    – Brad Bell
    Commented Jun 25, 2014 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.