<p>In Craft 3, <code>{{ _self.getTemplateName() }}</code> throws an error, but <code>{{ _self }}</code> does the trick.</p>

CC BY-SA 4.0
2018-09-05T11:34:30.720Z
2018-09-05T11:34:30.720Z
hashchange
<p>Post marked as answer doesn't work for me nowadays.
For me works:</p>
<pre><code>{{ craft.app.requestedParams.template }}
</code></pre>

CC BY-SA 4.0
2022-11-25T10:10:48.623Z
Thanks, works for me too (Craft 4).
GazCrane
CC BY-SA 4.0
2024-05-26T18:54:30.833Z
Thanks, works in CraftCMS 5 as well.
Martin Ille
CC BY-SA 4.0
2022-06-17T21:39:23.133Z
2022-06-17T21:39:23.133Z
Дмитрий Боровков
<p>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.</p>

CC BY-SA 3.0
2014-06-25T05:57:47.347Z
Thanks Brad, would you consider adding a tag for it a some point?
Gareth
CC BY-SA 3.0
2014-06-25T07:43:45.420Z
I don't see why not!
Brad Bell
CC BY-SA 3.0
2014-06-24T21:34:42.800Z
2014-06-24T21:34:42.800Z
Brad Bell
<p>This applies to Craft 2. For Craft 3+, see <a href="https://craftcms.stackexchange.com/a/27606/4748">hashchange's answer</a>.</p>
<hr />
<p>You can get the current template’s name like so:</p>
<pre><code>{{ _self.getTemplateName() }}
</code></pre>

CC BY-SA 3.0
2017-04-21T15:09:06.313Z
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
CC BY-SA 4.0
2014-07-07T21:35:28.417Z
2022-12-16T02:26:12.473Z
2022-12-16T02:26:12.473Z
Brandon Kelly
<p>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.</p>

CC BY-SA 3.0
2014-06-24T21:32:54.933Z
I don't think that's possible, but can you give an example why you want to check the template-name?
Victor
<templating><variable>
How can I make a template output its own filename?
# How can I make a template output its own filename?

(Asked by: username_0 on 2014-06-24)

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.

**Tags:** templating, variable

> **Comments:**
> **username_1**: I don't think that's possible, but can you give an example why you want to check the template-name?  

---

## 4 answers

### Answer by username_7 on 2014-07-07. Score: 15 | Accepted answer

This applies to Craft 2. For Craft 3+, see username_2's answer.

You can get the current template’s name like so:
{{ _self.getTemplateName() }}

> **Comments:**
> **username_8**: 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`  

---

### Answer by username_2 on 2018-09-05. Score: 10

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


---

### Answer by username_3 on 2022-06-17. Score: 6

Post marked as answer doesn't work for me nowadays.
For me works:
{{ craft.app.requestedParams.template }}

> **Comments:**
> **username_4**: Thanks, works for me too (Craft 4).  
> **username_5**: Thanks, works in CraftCMS 5 as well.  

---

### Answer by username_6 on 2014-06-24. Score: 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.

> **Comments:**
> **username_0**: Thanks Brad, would you consider adding a tag for it a some point?  
> **username_6**: I don't see why not!  

---

