2

i am trying to figure out the right way to display elements on a page. Its about a website for a fashion designer. There will be parent elements like :

  • Parent X
  • Parent Y

both of those elements use the same template and i used structure for it. i figured structure is the right choice because of the ability to sort them in the panel etc.

It should be possible to add child elements to the parents like

  • Parent X
    • Item a
    • Item b
  • Parent Y
    • Item c

etc..

those elements are using a different template:

Like - ParentTemplate/

and - ParentTemplate/ItemTemplate/

how can i structure them in the craft panel without making them a new structure, because the new structure isnt related to parents then anymore , what they have to be ... ot is it the wrong approach here ?

thanks a lot guys

1 Answer 1

4

Your approach looks good to me. There are different ways you can make it happen:

If you set up your structure in the CP you can decide how many levels you want to allow for that structure. (By default the number of levels is not limited.) If you have entered at least one parent entry in your structure, you can assign child entries in two ways:

1 – When you create a new entry you can choose a parent for that entry.

2 – If you miss that step or you change your mind later, you can move your entries around in the structure entries list: Choose your structure under „Entries“, klick and hold the cross in front of the entry you want to move and then drag it to the desired location. (So it´s possible to move a child entry from one parent to another later on.)

As for the templates for this set up, you have different options as described here: How to load a different template for parent page

Another possibly interesting solution would be „Entry types“.

If you find that your parent entries would also use different fields than your child entries it could make sense to define a second entry type for the children in that section (https://craftcms.com/docs/sections-and-entries). When you define the „child" entry type, you assign fields to that just like for the first (parent) entry type. If you now enter a new entry for the section you can choose between the entry types and then are presented with the different field sets.

Regarding the templates you can still use the first solution but now you could change the layouts based on entry types as well: In your template for single entries you could something like this

{% if entry.type == "child" %}
    {% include "sectionTemplateFolder/_types/child" %}
{% else %}
    {% include "sectionTemplateFolder/_types/parent" %}
{% endif %}

(This example assumes that you have an actual entry type called „child“, replace the „child“ in the if statement with your desired name.)

An elegant solution how to change layouts between several entry types is given here: https://craftcms.com/support/entry-type-templates

Whatever approach you take you do not need to structure your templates like your entries. Within the Craft templates folder, the templates can live where it makes the most sense to you. For the entry-type example it could be

  • sectionName (folder)
    • index.html (for listing)
    • _entry.html (for the single entries)
    • _types (folder)
      • child.html
      • parent.html

Hope this helps?

0

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.