I've got a Structure section where the top level is the main menu. The submenu consists of the children of the active main menu item. This works well.
For the submenu, I'm using this code:
{% set mainEntry = craft.entries.slug(craft.request.getSegment(1)).first() %}
{% set subnav = craft.entries.descendantOf(mainEntry) %}
<ul>
{% nav page in subnav %}
<li{% if page.slug == craft.request.getSegment(page.level) %} class="active"{% endif %}>
{{ page.getLink() }}
{% ifchildren %}
<ul>
{% children %}
</ul>
{% endifchildren %}
</li>
{% endnav %}
</ul>
...which shows all children of each page.
How can I only show the children of the active parent? I've tried to wrap the ifchildren
block inside another if
statement (the same I used to set the class="active"
to the <li>
), but that results in a Template error:
Unexpected tag name "ifchildren" (expecting closing tag for the "if" tag defined near line x)
Putting that if
statement inside the ifchildren
block also throws a template error.
PS. With the current HTML output, I could also just use CSS to hide/show the <ul>
s based on the .active
class, but I'd still like to know how to generate that tree without the need for a CSS workaround.