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?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityI 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:
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.