2

I wondered if it is possible to use one specific template for just one structure.

My structure build up

So let me make this clear. I have a structure called "Onze Diensten" (Our Services) and inside are child structures that are the actual services that the website offers. These child structures use a template that I assigned to them through structure so that works perfectly. But what I want now is that the actual "Onze Diensten" gets it's own individual template which would show all the services on one page.

How do I do that? Thank you for taking the time reading my question.

What I hoped would be the solution is using routes. By adding a route that when you visit onze-diensten/ it would load up the template onze-diensten/onze-diensten/ (Which is the path to the template I want to use).

But it seems to completely ignore the route at all.

3 Answers 3

4

I can't answer it clearly, but I did a workaround for something similar.

I've loaded the same template for my structure but gave my structure 2 entry-types(overviewPage, detailPage).

Both entry types loaded the same template but got handled differently. So my template for the structure looked something like this:

{% if entry.type == 'overviewPage' %}
  {% include 'snippets/content_overviewPage.html' %}
{% elseif entry.type == 'detailPage' %}
  {% include 'snippets/content_detailPage.html' %}
{% endif %}

I hope to see on this thread how other people handle this.

4
  • Thanks for your input! This explanation I can understand I also saw another thread on the same issue I believe and apparently someone gave a very good solution. But I don't understand what he did probably because I am still new at Craft. But maybe it can help you so here is [the link.] (craftcms.stackexchange.com/questions/96/…) Commented Mar 2, 2016 at 8:46
  • But how do you give different handles to the same structure? My structure handle is onzeDiensten and all the child structures get the same handle. Which means that I can use {% if entry.type == 'onzeDiensten' %} but I can't make use of the elseif beneath it because all the structures have the handle of onzeDiensten. Commented Mar 2, 2016 at 9:13
  • Make sure to define a second entry.type in your structure. And add this new entry.type you your entries. You can than handle those entries depending on their entry.type. Commented Mar 2, 2016 at 9:23
  • 1
    Marco, you are a genius. It works. I tried it failed a couple of times but eventually it worked out great! So thank you so much now I can finally move on with my project. Commented Mar 2, 2016 at 11:07
3

Building off of Marco's first answer:

In your case, perhaps the easiest approach is to have a conditional in your Structure's entry template that checks the entry's level property, and includes different templates based on that - i.e. if the entry's level is 1, that means the entry is top level, and Craft should load an overview template. If not, the single service template should be served.

Something like this should do the trick – I'm assuming you have a template folder called "services", with three templates: index.html (which is the Structure's entry template), overview.html and service.html, and the following would go inside the entry template:

{% if entry.level == 1 %}
    {% include 'services/overview' %}
{% else %}
    {% include 'services/service' %}
{% endif %}

Note that with this setup, you can just put "services" (without the quotes) for your Structure Section's Entry Template field; then Craft will load the "services/index.html" template whenever an entry from this Structure is requested, and the above conditional will make sure the correct template is served.

Another approach entirely is to create a dropdown field (called structureTemplate or the like) for your Structure, which is hard coded to the following values:

service : Service template
overview : Service overview template

This would enable you to select the template on an entry basis. Here's how you could handle this in your entry template:

{% set template = entry.structureTemplate|default('service') %}
{% include 'services/'~template %}

I really think the first approach is better for your use case, though – considering that top level entries should always load one template and child level entries should always load another.

2

I just noticed something.

"Onze Diensten" is your structure. In this structure you made an entry "Onze Diensten" on the first structure level.

If this "Onze Diensten" entry only is an overview of all it's children entries and doesn't contain any extra content you could just remove the "Onze Diensten" entry and make all the children entries on the first structure level.

For the overview page you would load the index.html with some code like this:

{% for entry in craft.entries.section('onzeDiensten') %}
  <a href={{entry.url}}>{{entry.title}}</a>
{% endfor %}
1
  • 1
    I need "Onze Diensten" To be an entry on it's own for the navigation plugin that I use. Otherwise I can't make a submenu with children in it. Commented Mar 2, 2016 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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