<p>If I understand you right, you could just use the Entry Model which is automatically populated as <code>entry</code> if you call the Index. Entry Model objects have a method <code>getSection()</code> which returns the Section Model to which the Entry Model object belongs.</p>

<p><a href="https://craftcms.com/docs/templating/entrymodel#getSection" rel="nofollow noreferrer">https://craftcms.com/docs/templating/entrymodel#getSection</a></p>

<p><strong>Edit:</strong>
Okay I think I didn't understand you right. I think you get the error because no section is related to your index template and instead you call it directly. You actually don't want that, so rename your index file to <code>_index</code> in order to prevent direct call. If you call it directly, no entry object is populated and voilà: error.</p>

<p>You can probably achieve what you want by utilizing the way Craft's example website <a href="https://github.com/pixelandtonic/HappyLager" rel="nofollow noreferrer">Happy Lager</a> does it:</p>

<ul>
<li>You have your channel sections with their handles <code>a, b, c...</code>. Entry Template of all is let's say <code>whatever/_entry</code>.</li>
<li>For every channel section you create a section of type single, the handles like <code>aIndex, bIndex, cIndex...</code>. All of these single sections point to <code>whatever/_index</code> as Entry Template but have different URIs, e.g. <code>a</code> for section <code>aIndex</code>.</li>
</ul>

<p>When you hit <code>craft.dev/a</code> now, you get your index template <code>whatever/_index</code> populated with <code>entry</code> and <code>entry.getSection()</code> returns a section model of <code>aIndex</code>. Now you can get all entries of channel section <code>a</code> by using</p>

<pre><code>{% set channel = entry.getSection().handle | replace("Index", "") %}
{% set entries = craft.entries.section(channel) %}
</code></pre>

<p>It is still kind of hacky because you have to name the section handles appropriately. </p>

CC BY-SA 3.0
2016-11-21T15:21:21.110Z
What exactly do you mean by "call the Index"? In my index template, I have tried `{% set x = entry.getSection() %}` but get an error "variable entry does not exist".
Steven Thate
CC BY-SA 3.0
2016-11-21T16:49:33.353Z
Awesome, thanks for clarifying that. I think I have a chance now. Thanks again :D
Steven Thate
CC BY-SA 3.0
2016-11-19T12:02:49.300Z
2016-11-21T16:19:50.697Z
2016-11-21T16:19:50.697Z
Johannes
<p>I have multiple sections (channels) that use the same index and _entry templates. <strong>In my index template, given that the section is dynamic, how do I define the <em>current</em> section and access it?</strong> Eg, I'd like to make the template's header dynamic: <code>&lt;h1&gt;{{ section.name ~ " Blog" }}&lt;/h1&gt;</code>.</p>

<p>Here is my fragile, hacked solution that I hope I never have to use in production:</p>

<p><em>I start things off by grabbing a section of the current URL. This returns a string, eg: "southwest-mn", that I use to load the actual section.</em></p>

<pre><code>{% set handle = craft.request.getSegment(2) %}
</code></pre>

<p><em>Next, I convert the string to one that reflects the section's handle format, eg: blog_southwest_mn (I changed it from blogSouthwestMn to make this string wizardry faster and easier)</em></p>

<pre><code>{% set sectionHandle = handle|replace('-', '_') %}
</code></pre>

<p><em>Now we can patch together a dynamic section name based on our URL, so let's grab the entries</em></p>

<pre><code>{% set blog = craft.entries.section('blog_' ~ sectionHandle) %}
</code></pre>

<p><em>This is the most embarrassing part of the code... I'm not sure how to get to the actual section model, so I used the following to display the correct header in the template</em></p>

<pre><code>{% if handle == "southwest-mn" %}
    {% set blogTitle = "Southwest MN Blog" %}
{% endif %}
</code></pre>

<p>That's how I was able to at least get a prototype template working. But <strong>how can I load a dynamic section's model</strong> so I don't have to write such madness?</p>

<p>Thanks!</p>

<templating><sections>
Many sections use one template: How do I get current section?
# Many sections use one template: How do I get current section?

(Asked by: username_0 on 2016-11-18)

I have multiple sections (channels) that use the same index and _entry templates. In my index template, given that the section is dynamic, how do I define the current section and access it? Eg, I'd like to make the template's header dynamic: {{ section.name ~ " Blog" }}.
Here is my fragile, hacked solution that I hope I never have to use in production:
I start things off by grabbing a section of the current URL. This returns a string, eg: "southwest-mn", that I use to load the actual section.
{% set handle = craft.request.getSegment(2) %}

Next, I convert the string to one that reflects the section's handle format, eg: blog_southwest_mn (I changed it from blogSouthwestMn to make this string wizardry faster and easier)
{% set sectionHandle = handle|replace('-', '_') %}

Now we can patch together a dynamic section name based on our URL, so let's grab the entries
{% set blog = craft.entries.section('blog_' ~ sectionHandle) %}

This is the most embarrassing part of the code... I'm not sure how to get to the actual section model, so I used the following to display the correct header in the template
{% if handle == "southwest-mn" %}
    {% set blogTitle = "Southwest MN Blog" %}
{% endif %}

That's how I was able to at least get a prototype template working. But how can I load a dynamic section's model so I don't have to write such madness?
Thanks!

**Tags:** templating, sections

---

## 1 answers

### Answer by username_1 on 2016-11-19. Score: 2 | Accepted answer

If I understand you right, you could just use the Entry Model which is automatically populated as entry if you call the Index. Entry Model objects have a method getSection() which returns the Section Model to which the Entry Model object belongs.
https://craftcms.com/docs/templating/entrymodel#getSection
Edit:
Okay I think I didn't understand you right. I think you get the error because no section is related to your index template and instead you call it directly. You actually don't want that, so rename your index file to _index in order to prevent direct call. If you call it directly, no entry object is populated and voilà: error.
You can probably achieve what you want by utilizing the way Craft's example website Happy Lager does it:

You have your channel sections with their handles a, b, c.... Entry Template of all is let's say whatever/_entry.
For every channel section you create a section of type single, the handles like aIndex, bIndex, cIndex.... All of these single sections point to whatever/_index as Entry Template but have different URIs, e.g. a for section aIndex.

When you hit craft.dev/a now, you get your index template whatever/_index populated with entry and entry.getSection() returns a section model of aIndex. Now you can get all entries of channel section a by using
{% set channel = entry.getSection().handle | replace("Index", "") %}
{% set entries = craft.entries.section(channel) %}

It is still kind of hacky because you have to name the section handles appropriately.

> **Comments:**
> **username_0**: What exactly do you mean by "call the Index"? In my index template, I have tried `{% set x = entry.getSection() %}` but get an error "variable entry does not exist".  
> **username_0**: Awesome, thanks for clarifying that. I think I have a chance now. Thanks again :D  

---

