1

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: <h1>{{ section.name ~ " Blog" }}</h1>.

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!

2

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.

  • 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 Nov 21 '16 at 15:21
  • Awesome, thanks for clarifying that. I think I have a chance now. Thanks again :D – Steven Thate Nov 21 '16 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.