<p>Craft can't really produce the uri patterns/routes you need automatically without giving it something to work with. With that said there are a couple of ways to approach this.</p>

<p>1) One option is to simply make your pages section into a structure; creating the needed hierarchy elements, and use it directly to generate your navigation. You may also want to specify additional 'entryType(s)' for header items, etc (i.e. items that don't have a corresponding page and/or are used exclusively for creating the nested route structure or for navigation headers, buttons, etc). You can then test <code>entry.type</code> in your template and deny or reroute the request accordingly.</p>

<p>2) Another option is to define a url format for your nav structure, which represents the uri pattern(s) that you would like to have (i.e. if the structure represents the site root then set the uri to <code>{slug}</code> and children to <code>{parent.uri}/{slug}</code>).</p>

<p>Using this approach, you would want to set up a template for your navigation structure which essentially <code>includes</code> the corresponding page template, as needed.</p>

<p>Something like this perhaps:</p>

<pre><code>{% if entry is defined %}
    {% set relatedPage = entry.contentLink.first %}
    {% type = relatedPage.type %}
    {% include ["pages/_types/" ~ type, "pages/_types/default"] with { 'entry': relatedPage } %}
{% else %}
    {% redirect '404' %}
{% endif %}
</code></pre>

<p>Notice that when including the page template, we are redefining the <code>entry</code> variable to be the related page, so that the 'page' template doesn't have to worry about it, and can be loaded directly or via the navigation template as needed. </p>

<p>Note: you may want to disable urls for your pages channel if you don't ever want users to access pages through those routes.</p>

CC BY-SA 3.0
2016-03-22T22:49:19.177Z
Thanks Douglas.  I see the dilemma I present when no Url format exists. The approach I took seems widespread but I thought I was missing something special. It seems I cant find a detailed example of building a nested structure navigation with hierarchical url routes that have active parent and child items.

I think option 1 sounds like i might need some singles as 'index' pages to channels when there is a bit more content required above a list of entries.
joomkit
CC BY-SA 3.0
2016-03-22T22:54:07.850Z
And no 2 sounds like it might be my solution for now - a pure navigation template. I am not sure I quite understand the purpose of redefining entry as 'related' - what problem does this solve?
joomkit
CC BY-SA 3.0
2016-03-22T23:09:20.553Z
If you did not redefine 'entry', then when you included the page template, the page template would have to reference content via the relatedPage variable (i.e. `{{ entry.relatedPage.title }}` ) — the 'entry' variable would refer to the navigation entry, not the related page entry. Redefining it would allow the page template to refer to `entry` as if it had been requested directly. Does that make sense?
Douglas McDonald
CC BY-SA 3.0
2016-03-22T23:17:10.673Z
As to your first question, I personally like to use option 1 for most of my projects. I typically use a 'pages' structure that represents all top level pages in the site, including index pages. I then use separate sections/channels to hold repeating content (i.e. products, news, case studies, projects, people, whatever the case may be), and set up their Url structures to seemlessly mesh with the pages structure. (i.e. pages would define the uri 'news'.. and the channel 'news' would begin with the uri `news/{slug}`.
Douglas McDonald
CC BY-SA 3.0
2016-03-23T09:52:53.510Z
Thanks - i get the need for redefining entry. Still figuring out the rest ;)
joomkit
CC BY-SA 3.0
2016-03-22T19:38:03.787Z
2016-03-22T19:38:03.787Z
Douglas McDonald
<p>I am using a structure for navigation. The structure has two fields :
entry field: 'contentLink'
custom link: 'customLink'</p>

<p>These are used to create a menu structure like:
About
- sub item 1
- sub item 2
- sub item 3</p>

<p>The section for this is set to have no url templates setup.</p>

<p>The entry links for sub item 1 to 3 point at entries in a page structure called 'pages' which is currently flat (ie no children)</p>

<p>In my template i am using a drop-down menu and code to get child items which works OK. 
    {% set menuentries = craft.entries.section('topnav') %}</p>

<pre><code>{% for entry in menuentries %}

    {% if entry.contentLink.first|length %}
        {# attempt to set navlink var #}
        {% set navlink = entry.contentLink.first().slug %}

    {% elseif entry.customLink|length %}

        {% set navlink = entry.customLink %}

    {% endif %}

     {% if entry.level == 1 and entry.children|length %}
        &lt;li class="dropdown"&gt;
            &lt;a href="{{ entry.url }}" class="dropdown-toggle" data-toggle="dropdown"&gt;{{ entry.title }} &lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;

            &lt;ul class="dropdown-menu" role="menu"&gt;
                {% for child in entry.getDescendants() %}
                    &lt;li class=""&gt;
                        &lt;a href="{{ siteUrl }}{{ child.slug }}"&gt;{{ child.title }}&lt;/a&gt;
                    &lt;/li&gt;
            {% endfor %}
             &lt;/ul&gt;
         &lt;/li&gt;

    {% elseif entry.level == 1 and not entry.hasDescendants() %}

         &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
    {% endif %}
{% endfor %}
</code></pre>

<p>But i cant figure out how to get:
a) a url for child.slug that reflects the heirarchy of the menu structure
b) parent and child active classes for highlighting / breadcrumb purposes</p>

<p>question restated:
How can I output child urls that reflect the a structured menu hierarchy like: 'about/sub item1' when the structure has no URL format specified?</p>

<structure><navigation>
How can I output child urls that reflect a structured menu hierarchy when the structure has no URL format specified?
# How can I output child urls that reflect a structured menu hierarchy when the structure has no URL format specified?

(Asked by: username_0 on 2016-03-22)

I am using a structure for navigation. The structure has two fields :
entry field: 'contentLink'
custom link: 'customLink'
These are used to create a menu structure like:
About
- sub item 1
- sub item 2
- sub item 3
The section for this is set to have no url templates setup.
The entry links for sub item 1 to 3 point at entries in a page structure called 'pages' which is currently flat (ie no children)
In my template i am using a drop-down menu and code to get child items which works OK. 
    {% set menuentries = craft.entries.section('topnav') %}
{% for entry in menuentries %}

    {% if entry.contentLink.first|length %}
        {# attempt to set navlink var #}
        {% set navlink = entry.contentLink.first().slug %}

    {% elseif entry.customLink|length %}

        {% set navlink = entry.customLink %}

    {% endif %}

     {% if entry.level == 1 and entry.children|length %}
        
            {{ entry.title }} 

            
                {% for child in entry.getDescendants() %}
                    
                        {{ child.title }}
                    
            {% endfor %}
             
         

    {% elseif entry.level == 1 and not entry.hasDescendants() %}

         {{ entry.title }}
    {% endif %}
{% endfor %}

But i cant figure out how to get:
a) a url for child.slug that reflects the heirarchy of the menu structure
b) parent and child active classes for highlighting / breadcrumb purposes
question restated:
How can I output child urls that reflect the a structured menu hierarchy like: 'about/sub item1' when the structure has no URL format specified?

**Tags:** structure, navigation

---

## 1 answers

### Answer by username_1 on 2016-03-22. Score: 1

Craft can't really produce the uri patterns/routes you need automatically without giving it something to work with. With that said there are a couple of ways to approach this.
1) One option is to simply make your pages section into a structure; creating the needed hierarchy elements, and use it directly to generate your navigation. You may also want to specify additional 'entryType(s)' for header items, etc (i.e. items that don't have a corresponding page and/or are used exclusively for creating the nested route structure or for navigation headers, buttons, etc). You can then test entry.type in your template and deny or reroute the request accordingly.
2) Another option is to define a url format for your nav structure, which represents the uri pattern(s) that you would like to have (i.e. if the structure represents the site root then set the uri to {slug} and children to {parent.uri}/{slug}).
Using this approach, you would want to set up a template for your navigation structure which essentially includes the corresponding page template, as needed.
Something like this perhaps:
{% if entry is defined %}
    {% set relatedPage = entry.contentLink.first %}
    {% type = relatedPage.type %}
    {% include ["pages/_types/" ~ type, "pages/_types/default"] with { 'entry': relatedPage } %}
{% else %}
    {% redirect '404' %}
{% endif %}

Notice that when including the page template, we are redefining the entry variable to be the related page, so that the 'page' template doesn't have to worry about it, and can be loaded directly or via the navigation template as needed. 
Note: you may want to disable urls for your pages channel if you don't ever want users to access pages through those routes.

> **Comments:**
> **username_0**: Thanks Douglas.  I see the dilemma I present when no Url format exists. The approach I took seems widespread but I thought I was missing something special. It seems I cant find a detailed example of building a nested structure navigation with hierarchical url routes that have active parent and child items.  I think option 1 sounds like i might need some singles as 'index' pages to channels when there is a bit more content required above a list of entries.  
> **username_0**: And no 2 sounds like it might be my solution for now - a pure navigation template. I am not sure I quite understand the purpose of redefining entry as 'related' - what problem does this solve?  
> **username_1**: If you did not redefine 'entry', then when you included the page template, the page template would have to reference content via the relatedPage variable (i.e. `{{ entry.relatedPage.title }}` ) — the 'entry' variable would refer to the navigation entry, not the related page entry. Redefining it would allow the page template to refer to `entry` as if it had been requested directly. Does that make sense?  
> **username_1**: As to your first question, I personally like to use option 1 for most of my projects. I typically use a 'pages' structure that represents all top level pages in the site, including index pages. I then use separate sections/channels to hold repeating content (i.e. products, news, case studies, projects, people, whatever the case may be), and set up their Url structures to seemlessly mesh with the pages structure. (i.e. pages would define the uri 'news'.. and the channel 'news' would begin with the uri `news/{slug}`.  
> **username_0**: Thanks - i get the need for redefining entry. Still figuring out the rest ;)  

---

