<p>Maybe not the solution you was looking for, but here is an alternative solution I'm using.</p>

<p>Put a Twig block in your "master template" (_layout.html?) like this:</p>

<pre><code>&lt;body class="{% block bodyClass %}{% endblock bodyClass %}"&gt;
</code></pre>

<p>And from my templates I can <strong>add</strong> classes to the body tag like this:</p>

<pre><code>{% block bodyClass %}{{ parent() }} news-index anotherclass{% endblock bodyClass %}
</code></pre>

<p>Or <strong>replace</strong> the classes by omitting the parent() like this:</p>

<pre><code>{% block bodyClass %}news-index anotherclass{% endblock bodyClass %}
</code></pre>

CC BY-SA 3.0
2014-12-17T10:14:29.197Z
Thanks, but I already have something like this, I am using a variable rather than a block which gets set before `@extends` but I was hoping to avoid having to explicitly set the class for each of my templates, would have been nice if this could be automated.
Lea Hayes
CC BY-SA 3.0
2014-12-17T07:49:11.647Z
2014-12-17T07:49:11.647Z
Alf Vestre
<p>You can use <code>{% set templateName = _self.getTemplateName() %}</code> from <code>_edit</code> and pass that up as a variable that your layout template can utilize.</p>

CC BY-SA 3.0
2014-12-17T10:12:38.827Z
I have tried this but it does not seem to return the name of the template that is dealing with the request, but rather the name of the current template; '_layout.html' in my case.
Lea Hayes
CC BY-SA 3.0
2014-12-17T16:02:52.030Z
And you put the `getTemplateName()` call inside of the `_edit` template?
Brad Bell
CC BY-SA 3.0
2014-12-17T02:53:55.003Z
2014-12-17T02:53:55.003Z
Brad Bell
<p>For convenience it would be rather useful if there was a way for me to achieve the following inside my main layout template:</p>

<pre><code>&lt;body class="template--{{ getNameOfPageTemplateHere|replace({ '/':'-', '_':'' }) }}"&gt;
</code></pre>

<p>Such that a page rendered using the template "blog/_entry" would have the following body tag:</p>

<pre><code>&lt;body class="template--blog-entry"&gt;
</code></pre>

<p>Is this possible?</p>

<templating>
How to get name of template that was chosen for request?
# How to get name of template that was chosen for request?

(Asked by: username_0 on 2014-12-17)

For convenience it would be rather useful if there was a way for me to achieve the following inside my main layout template:


Such that a page rendered using the template "blog/_entry" would have the following body tag:


Is this possible?

**Tags:** templating


---

## 2 answers

### Answer by username_1 on 2014-12-17. Score: 2

Maybe not the solution you was looking for, but here is an alternative solution I'm using.
Put a Twig block in your "master template" (_layout.html?) like this:


And from my templates I can add classes to the body tag like this:
{% block bodyClass %}{{ parent() }} news-index anotherclass{% endblock bodyClass %}

Or replace the classes by omitting the parent() like this:
{% block bodyClass %}news-index anotherclass{% endblock bodyClass %}

> **Comments:**
> **username_0**: Thanks, but I already have something like this, I am using a variable rather than a block which gets set before `@extends` but I was hoping to avoid having to explicitly set the class for each of my templates, would have been nice if this could be automated.  

---

### Answer by username_2 on 2014-12-17. Score: 1

You can use `{% set templateName = _self.getTemplateName() %}` from `_edit` and pass that up as a variable that your layout template can utilize.

> **Comments:**
> **username_0**: I have tried this but it does not seem to return the name of the template that is dealing with the request, but rather the name of the current template; '_layout.html' in my case.  
> **username_2**: And you put the `getTemplateName()` call inside of the `_edit` template?  

---

