3

I read the documentation but I'm still confused about the Structure sections..

What they are usually used for? What are the advantages of using it? And most important how to use them?

Please can you explain with some examples?

11

I don't know if its possible to be much clearer than the official docs on this matter, but basically a Structure is needed if your entries needs to be in a specific order (and you want to manage that order w/ an easy to use, drag-n-drop interface within the Control Panel), or if they need to be nested (i.e. hierarchical, with parent/child entries).

The alternative to a Structure is a Channel. A Channel doesn't have the ability to nest entries – in other words, it's a flat stream of content – nor can entries be sorted in a specific order; you're stuck with sorting by published date, title, ID or other "standard" meta data. The best example of a good use case for a Channel I can think of is a blog.

If you create a Channel and later realize that it should've been a Structure, you can convert it by visiting that Section's settings and choosing "Structure" for its type. You can also convert a Structure to a Channel, although if you later convert it back to a Structure your ordering/nesting data will be lost.

Some good use cases for a Structure could be:

  • A "Documentation" type section, with "main" entries (e.g. "Javascript") and sub entries (e.g. "Javascript / jQuery"). The official Craft docs are built using a Structure, where child entries (e.g. "Requirements") are nested under parent entries (e.g. "Installing and updating"). See this answer from Brad Bell for a bit more information on how P&T built the docs.
  • A "Navigation" type section – some people like to build their site's navigation using a Structure, which makes it easy to edit the order of the nav items, create dropdowns/sub menus etc. Here's a thread with some information more information on this approach.
  • A "Recipes" section, where you could easily sort the different recipes after course types (meat/vegetarian)

As for using Structures, I'm assuming you're talking about how to display the entries in a template – as the usage within the Control Panel should be fairly self-explanatory. Basically, outputting entries from a Structure is no different from the other section types:

{% for entry in craft.entries.section('myStructureSectionHandle') %}
    ...
{% endfor %}

However, if your Structure's entries are nested, using the nav tag instead of the basic for loop can be helpful in displaying the hierarchical content, because it can recursively output child entries:

{% nav entry in craft.entries.section('myStructureSectionHandle') %}
    <li>
        <a href="{{ entry.url }}">{{ entry.title }}</a>
        {% ifchildren %}
            <ul>
                {% children %}
            </ul>
        {% endifchildren %}
    </li>
{% endnav %}

Beyond this, I think your question is a bit too open-ended to answer in detail, and you should probably create separate questions for any specific issues or problems you encounter working with Structures.

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.