Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-17T10:14:29.197Z
Answers.Text: 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.
Answers.UserDisplayName: Lea Hayes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-17T07:49:11.647Z
Answers.LastActivityDate: 2014-12-17T07:49:11.647Z
Answers.UserDisplayName: Alf Vestre
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-17T10:12:38.827Z
Answers.Text: 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.
Answers.UserDisplayName: Lea Hayes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-17T16:02:52.030Z
Answers.Text: And you put the `getTemplateName()` call inside of the `_edit` template?
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-17T02:53:55.003Z
Answers.LastActivityDate: 2014-12-17T02:53:55.003Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <templating>
Title: How to get name of template that was chosen for request?
ThreadText: # 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:
  
  ```
  <body class="template--{{ getNameOfPageTemplateHere|replace({ '/':'-', '_':'' }) }}">
  
  ```
  
  Such that a page rendered using the template "blog/\_entry" would have the following body tag:
  
  ```
  <body class="template--blog-entry">
  
  ```
  
  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:
  
  ```
  <body class="{% block bodyClass %}{% endblock bodyClass %}">
  
  ```
  
  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?  
  
  ---
  
  