4

I am wondering if there is a best practice around using channels and structures. When you have a single section you can link to that section by selecting the entry for it. For channels and structures there is no "entry" that represents the landing page where I just want to display the list of entries. Do I need to create a single section as the landing page and loop entries from my channel section in order to link to that landing page?

  • There is an "entries" field which gives you the abillity to select any entries including structures and channels which you can display from there on. If this is not what you are looking for could you be more clear? – Justin Dekkers Oct 19 '16 at 13:54
  • I reworded my issue – jpoiri Oct 19 '16 at 14:21
7

You basically have two options for creating a "listing" or "archive" view for your Channel or Structure sections.

The first option is to create a Single. This is in many cases appropriate, e.g. if you need to add content to the listing view in addition to the actual entry list, such as a visual banner, introductory text etc.

Inside the Single section's entry template, you'll need to pull the entries you want to display in the list using craft.entries – here's a very basic example for how the Single section's entry template could look:

{% extends '_layout' %}

{% block content %}

    {# Display some content from our Single #}
    <h1>{{ entry.title }}</h1>
    <p>{{ entry.introduction }}</p>

    {# ...and a list of entries in the "news" Section #}
    <h2>Entries</h2>
    <ul>
        {% for newsEntry in craft.entries.section('news') %}
            <li><a href="{{ newsEntry.url }}">{{ newsEntry.title }}</a></li>
        {% endfor %}
    </ul>

{% endblock %}

The second option is to not create a Single :) Of course, then you need to solve the problem of there not being an actual URL for your "archive" or "list" view.

There are many ways to approach this, but a simple and good solution to this problem is to leverage Craft's template routing pattern. If your Channel or Structure section's URL format is news/{slug}, and the entry template is news/_entry.html, you can simply create a template called news/index.html, and Craft will render that template for any requests to example.com/news.

Without a Single, the templates/news/index.html template will look much the same as the above example (you'll still need the craft.entries query to actually pull the entries) albeit without a pre-populated entry variable:

{% extends '_layout' %}

{% block content %}

    <h1>News archive</h1>
    <p>Here's our awesome news archive</p>

    <h2>Entries</h2>
    <ul>
        {% for entry in craft.entries.section('news') %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
    </ul>

{% endblock %}

About "Craft's template routing pattern" mentioned above; these are two important things to know about:

– Whenever Craft can't match a URL request to an entry or category (and also no dynamic routes) it'll look for a matching template path. This means that if there's no entry or category with a URL format matching the request example.com/news, Craft will look for a template with the filepath /templates/news/index.html. If that template exists, Craft will render it.

– Any template prefixed with an underscore (or in a folder prefixed with an underscore), such as /templates/news/_entry, is excluded from template routing. This means that Craft won't match the URL example.com/news/entry to the news/_entry.html template.

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.