Answers.Body: <p>One thing that’s important to remember is that there’s no inherent relationship between your templates and sections. Any template can show any entry from any section. So there’s no such thing as the “current section”.</p>
  
  <p>If your section has the “Entries in this section have their own URLs” setting checked, and you’re requesting one of that section’s entries’ URLs, then the section’s template will get loaded automatically and an <code>entry</code> variable will be available to it, which represents the entry whose URL you requested. But that doesn’t mean that you couldn’t have loaded the same template some other way (e.g. via an <a href="http://buildwithcraft.com/docs/templating/craft.entries"><code>{% include %}</code></a> tag from another template), or that you couldn’t have manually fetched that entry some other way (e.g. with <a href="http://buildwithcraft.com/docs/templating/craft.entries"><code>craft.entries</code></a>).</p>
  
  <p>Anyway, if you have an <a href="http://buildwithcraft.com/docs/templating/entrymodel">EntryModel</a> variable set (such as that <code>entry</code> variable that you’d automatically get when accessing an entry’s URL), you can access it’s section using <a href="http://buildwithcraft.com/docs/templating/entrymodel#getSection"><code>entry.getSection()</code></a>, and you can access that section’s handle via its <a href="http://buildwithcraft.com/docs/templating/sectionmodel#handle"><code>handle</code></a> property.</p>
  
  
  
  <pre class="lang-twig prettyprint-override"><code>{% set section = entry.getSection() %}
  {% set handle = section.handle %}
  </code></pre>
  
  <p>That could be simplified to:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set handle = entry.getSection().handle %}
  </code></pre>
  
  <p>or even:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set handle = entry.section.handle %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-16T14:17:11.790Z
Answers.Text: I know this is an old question, but I can't get this to work properly in a multisite environment. All I get out is the "main" handle, when I need to localized handle.
Answers.UserDisplayName: InanisAtheos
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-08T11:15:08.937Z
Answers.Text: Nice, thank you!
  While this works for exactly what I need... I can see why this isn't always the best approach to assign each page with a generic class. The idea is just help with styling. 
  
  I wonder (for future reference), how I might be able to get the same information via a plugin? Do you know of a PHP method?
  
  Thanks again :)
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-08T12:35:10.130Z
Answers.Text: You can get the matched entry [like so](http://craftcms.stackexchange.com/questions/1715/how-can-i-get-the-requested-entry-from-a-plugin/1716#1716), and if there is one, you can access its section’s handle via `$element->getSection()->handle`.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T10:40:04.307Z
Answers.LastActivityDate: 2014-08-07T10:40:04.307Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I can't help but think this is a silly/obvious question, but I'm trying to retrieve the current section handle so I can use it as a class. </p>
  
  <p>Maybe I'm going about it wrong, but {{ handle }} does nothing. </p>
  
  <p>Any ideas?</p>
  
Tags: <templating><entry><sections>
Title: Get the current section handle
ThreadText: # Get the current section handle
  
  (Asked by: username_0 on 2014-08-07)
  
  I can't help but think this is a silly/obvious question, but I'm trying to retrieve the current section handle so I can use it as a class.
  
  Maybe I'm going about it wrong, but {{ handle }} does nothing.
  
  Any ideas?
  
  **Tags:** templating, entry, sections
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-08-07. Score: 23 | Accepted answer
  
  One thing that’s important to remember is that there’s no inherent relationship between your templates and sections. Any template can show any entry from any section. So there’s no such thing as the “current section”.
  
  If your section has the “Entries in this section have their own URLs” setting checked, and you’re requesting one of that section’s entries’ URLs, then the section’s template will get loaded automatically and an `entry` variable will be available to it, which represents the entry whose URL you requested. But that doesn’t mean that you couldn’t have loaded the same template some other way (e.g. via an [`{% include %}`](http://buildwithcraft.com/docs/templating/craft.entries) tag from another template), or that you couldn’t have manually fetched that entry some other way (e.g. with [`craft.entries`](http://buildwithcraft.com/docs/templating/craft.entries)).
  
  Anyway, if you have an [EntryModel](http://buildwithcraft.com/docs/templating/entrymodel) variable set (such as that `entry` variable that you’d automatically get when accessing an entry’s URL), you can access it’s section using [`entry.getSection()`](http://buildwithcraft.com/docs/templating/entrymodel#getSection), and you can access that section’s handle via its [`handle`](http://buildwithcraft.com/docs/templating/sectionmodel#handle) property.
  
  ```twig
  {% set section = entry.getSection() %}
  {% set handle = section.handle %}
  
  ```
  
  That could be simplified to:
  
  ```twig
  {% set handle = entry.getSection().handle %}
  
  ```
  
  or even:
  
  ```twig
  {% set handle = entry.section.handle %}
  
  ```
  
  > **Comments:**
  > **username_0**: Nice, thank you! While this works for exactly what I need... I can see why this isn't always the best approach to assign each page with a generic class. The idea is just help with styling.   I wonder (for future reference), how I might be able to get the same information via a plugin? Do you know of a PHP method?  Thanks again :)  
  > **username_1**: You can get the matched entry [like so](http://craftcms.stackexchange.com/questions/1715/how-can-i-get-the-requested-entry-from-a-plugin/1716#1716), and if there is one, you can access its section’s handle via `$element->getSection()->handle`.  
  > **username_2**: I know this is an old question, but I can't get this to work properly in a multisite environment. All I get out is the "main" handle, when I need to localized handle.  
  
  ---
  
  