<p>If you want to be able to add a class="active" to the selected URL you can use Twig's replace filter. Since the code will be cached you can't do this at runtime.</p>

<pre><code>{% set navigation %}
{% cache globally for 3 years %}
{% set entries = craft.entries.section('navigation') %}

&lt;ul&gt;
{% nav entry in entries %}
    &lt;li&gt;

        {# Check for entry type / get related entry #}
        {% if entry.type == 'linkedChannelEntry' %}
            {% set linkedEntry = entry.entriesField.limit(1).first() %}
        {% endif %}

        {% if linkedEntry %}

            {# Link to a linked channel entry #}
            &lt;a href="{{ linkedEntry.url }}" data-url="{{ linkedEntry.url }}"&gt;{{ linkedEntry.title }}&lt;/a&gt;

        {% else %}

            {# Link to a structure entry #}
            &lt;a href="{{ entry.url }}" data-url="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;

        {% endif %}

        {# Repeat this for child entries #}
        {% ifchildren %}
            &lt;ul&gt;{% children %}&lt;/ul&gt;
        {% endifchildren %}

    &lt;/li&gt;
{% endnav %}
&lt;/ul&gt;
{% endcache %}
{% endset %}

{{ navigation|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }}
</code></pre>

CC BY-SA 3.0
2014-07-30T20:02:49.047Z
Really clever idea! You could also do a separate Q/A only for this particular problem (simplified or better: completely generalized), naboo. It's probably somewhat hidden in here and could come handy in many situations where caching is required, not only for adding a class to a navigation. Good work!
carlcs
CC BY-SA 3.0
2014-07-30T13:56:59.763Z
2014-07-30T13:56:59.763Z
naboovalley
<p><strong>Structure for "sorting-only"</strong></p>

<p>I haven't tried the approach that you are thinking of yet, but your concerns regarding the query count are not that unfounded. You would need to use the <a href="http://buildwithcraft.com/docs/relations"><code>relatedTo()</code></a> parameter with the ElementCriteriaModel you create <s>on your pages (to get the single's content)</s>
on your navigation <a href="http://twig.sensiolabs.org/doc/tags/include.html">include</a> (even if you only want to get the entries URL) and relations can have a rather big performance hit. <s>Hopefully this is not true for a simple relation like this; otherwise, you'd still have the <a href="http://buildwithcraft.com/docs/templating/cache#when-to-use-cache-tags"><code>cache</code></a> tag.</s> But I'd suggest you to <a href="http://buildwithcraft.com/docs/templating/cache#when-to-use-cache-tags">cache</a> the whole include (this is a perfect example for caching being really useful). Additionally setting the limit to '1' will probably help to keep the query count low if the <a href="http://buildwithcraft.com/docs/templating/cache#cache-clearing">cache is cleared</a>.</p>



<pre class="lang-twig prettyprint-override"><code>{% cache for 3 years %}

    {% set pages = craft.entries.section('pagesSectionHandle').level(1) %}

    {% for page in pages %}

        {% set params = {
            relatedTo: { sourceElement: page, field: "entriesFieldHandle" },
            limit: '1'
        } %}

        {% set linkedEntry = craft.entries(params).first() %}

        {# Get the title from the structure (why set this twice?) #}
        &lt;a href="{{ linkedEntry.url }}"&gt;{{ page.title }}&lt;/a&gt;

        {# Get the title from the entry #}
        &lt;a href="{{ linkedEntry.url }}"&gt;{{ linkedEntry.title }}&lt;/a&gt;

    {% endfor %}

{% endcache %}
</code></pre>

<p><strong>Structure containing "all the content"</strong></p>

<p>What I'd suggest you to do is to not only use the structure as a "sorting only" structure, but to also add content directly to it.</p>

<p>Use entry types for your singles, if you need to load individual templates for them, and further entry types for your channel index pages. If it's thought-out carefully (modular site design) you probably won't end up with lots of entry types.</p>

<p><strong>Combining those two</strong></p>

<p>If you have a need to link to specific channel entries from your navigation, you could make an entry type in your structure specifically for those links to channel entries. All other content could still be added directly to the structure itself as described above.</p>

<p>I think that being able to edit the content directly in the pages structure is a very convenient and less geeky like experience for the client.</p>

<p><em>Edit:</em></p>

<p>This is what Naboovalley and I came up with in the chat (we should have done this before spamming the comments section). It "will give you a fully dynamic menu for your client linking between Singles, Channels and even other Structure entries":</p>

<pre class="lang-twig prettyprint-override"><code>{% cache globally for 3 years %}
{% set entries = craft.entries.section('navigation') %}

&lt;ul&gt;
    {% nav entry in entries %}
        &lt;li&gt;

            {# Check for entry type / get related entry #}
            {% if entry.type == 'linkedChannelEntry' %}
                {% set linkedEntry = entry.entriesField.limit(1).first() %}
            {% endif %}

            {% if linkedEntry %}

                {# Link to a linked channel entry #}
                &lt;a href="{{ linkedEntry.url }}"&gt;{{ linkedEntry.title }}&lt;/a&gt;

            {% else %}

                {# Link to a structure entry #}
                &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;

            {% endif %}

            {# Repeat this for child entries #}
            {% ifchildren %}
                &lt;ul&gt;{% children %}&lt;/ul&gt;
            {% endifchildren %}

        &lt;/li&gt;
    {% endnav %}
&lt;/ul&gt;
{% endcache %}
</code></pre>

CC BY-SA 3.0
2014-07-29T09:06:35.383Z
I would only be interested in the page.link as the relation, no content. It's basically using a Structure to make a navigation tree of slugs to all different sections. Let's say my client want to have a navigation where he/she links to 3 singles and 10 entries from different channels.
naboovalley
CC BY-SA 3.0
2014-07-29T09:50:28.980Z
@naboovalley ok. I changed my answer a bit and added example code to better see if I got you right.
carlcs
CC BY-SA 3.0
2014-07-29T16:42:13.977Z
Yeah that's basically what I was looking for, but with the exception of not using the "level(1)" since I want the client to be able to build a <ul><li></li></ul> with as many depths as it might like. I tried your code and as you'd expected it's not really optimal since it adds queries for each related linked field. You could say I'm looking for the same functionality as "Taxonomy" for EE - "one navigation to rule them all". I'm guessing a Structure isn't the best way to achieve this.
naboovalley
CC BY-SA 3.0
2014-07-29T16:46:37.860Z
New idea! How about I build a Plugin that let's me add a URL/slug link to another entry and each time it's fetched fetches the entire current Structure links and cache them during page load to only make it use 1 query for each Structure loop. Is there any easy way for me to build the <ul><li></li></ul> list without using the {% nav %} tag for a Structure?
naboovalley
CC BY-SA 3.0
2014-07-29T17:02:37.263Z
I wouldn't overcomplicate this, @naboovalley. As I wrote, I'd definitely add all the content (all what you'd normally put in singles) in the structure itself, then I'd make a `_navigation.html` template as an include and apply the `cache` tag to the whole navigation loop. Try it without caching (with the params I pointed out), maybe it is not that query heavy as you think. But if it is, this will only happen once (because of the 3yrs caching time) after you did an edit to the navigation!
carlcs
CC BY-SA 3.0
2014-07-29T17:05:55.453Z
But 3 years caching.... The client might change this Structure 10 times a day and want to check the output directly after. Also don't you find it "wierd" to have the client add "News entries" to a Structure called "Navigation"? (just an example) :)
naboovalley
CC BY-SA 3.0
2014-07-29T17:07:40.580Z
It gets updated automatically as soon as your template would change!
carlcs
CC BY-SA 3.0
2014-07-29T17:08:27.230Z
@naboovalley I have no experience with EE, but I've set up a comparable navigation structure and I'd say that a structure is definitely a really great way to achieve what you want.
carlcs
CC BY-SA 3.0
2014-07-29T17:12:59.303Z
@naboovalley is "News entries" a channel or a single? Channels entries would still be their own sections (in the CP) but you could add the channels' index page to the structure. And why would you name the structure "Navigation", I'd say "Pages" is more appropriate if all the pages (besides channel entries) with all the content live in there ;)
carlcs
CC BY-SA 3.0
2014-07-29T17:15:18.430Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/16044/discussion-between-naboovalley-and-christian-seelbach).
naboovalley
CC BY-SA 3.0
2014-07-29T19:02:44.263Z
@naboo, I added the `limit(1)` parameter and a conditional for the `entry.type`. This should further reduce your potential queries!
carlcs
CC BY-SA 3.0
2014-07-28T17:16:56.223Z
2014-07-29T18:57:43.587Z
2014-07-29T18:57:43.587Z
carlcs
<p>I'm setting up my first Craft project and I have a lot of different content. I have around 6 different Channels (News, Events, Blog, Pages, Products, Staff) and ~10 Singles.</p>

<p>I have this idea of using Structures as my sorting and "Navigation" "content types" only. Meaning I'll for example have one Structure called "Navigation" in which I have the default "Title" field and one "Entries" field (for linking to other entries in channels and singles). This I'll output in my template and fetching each linked entries page.link to build my main site navigation.</p>

<p>This should give my client the ability to control the main navigation structure of the entire site 100% with sorting, linking and naming each navigation item ("Title").</p>

<p>Is this a good approach on using a Structure this way? Or will the extra field "Entries" give me a massive query count when the Structure contains (let's say) 100 entries?</p>

CC BY-SA 3.0
2014-07-28T20:02:54.243Z
Just made an edit to your question title, naboovalley. You don't want the channels' entries in you nav, right?
carlcs
CC BY-SA 3.0
2014-07-29T09:07:15.847Z
Depends on what you mean :)
I want my client to create a navigation tree (Structure..) with the ability to link to individual entries across multiple channels and singles.
naboovalley
CC BY-SA 3.0
2014-07-29T10:23:11.560Z
Made another edit ;)
carlcs
CC BY-SA 3.0
2014-07-29T18:30:06.510Z
Naboo, we should keep answers an questions separate with Stack Exchange. If you don't mind, I'd update my answer with the `{% nav %}` part worked in (even if the "run the relations query in the `{% nav %}` loop" idea was yours)!
carlcs
CC BY-SA 3.0
2014-07-29T19:10:15.397Z
@carlcs perfect! :)
naboovalley
<navigation><structure>
Would you use a Structure as navigation over multiple sections (channel entries, indexes and singles)?
# Would you use a Structure as navigation over multiple sections (channel entries, indexes and singles)?

(Asked by: username_0 on 2014-07-28)

I'm setting up my first Craft project and I have a lot of different content. I have around 6 different Channels (News, Events, Blog, Pages, Products, Staff) and ~10 Singles.

I have this idea of using Structures as my sorting and "Navigation" "content types" only. Meaning I'll for example have one Structure called "Navigation" in which I have the default "Title" field and one "Entries" field (for linking to other entries in channels and singles). This I'll output in my template and fetching each linked entries page.link to build my main site navigation.

This should give my client the ability to control the main navigation structure of the entire site 100% with sorting, linking and naming each navigation item ("Title").

Is this a good approach on using a Structure this way? Or will the extra field "Entries" give me a massive query count when the Structure contains (let's say) 100 entries?

**Tags:** navigation, structure
> **Comments:**
> **username_1**: Just made an edit to your question title, naboovalley. You don't want the channels' entries in you nav, right?  
> **username_0**: Depends on what you mean :) I want my client to create a navigation tree (Structure..) with the ability to link to individual entries across multiple channels and singles.  
> **username_1**: Made another edit ;)  
> **username_1**: Naboo, we should keep answers an questions separate with Stack Exchange. If you don't mind, I'd update my answer with the `{% nav %}` part worked in (even if the "run the relations query in the `{% nav %}` loop" idea was yours)!  
> **username_0**: @carlcs perfect! :)  

---

## 2 answers

### Answer by username_1 on 2014-07-28. Score: 12 | Accepted answer

Structure for "sorting-only"
I haven't tried the approach that you are thinking of yet, but your concerns regarding the query count are not that unfounded. You would need to use the relatedTo() parameter with the ElementCriteriaModel you create on your pages (to get the single's content)
on your navigation include (even if you only want to get the entries URL) and relations can have a rather big performance hit. Hopefully this is not true for a simple relation like this; otherwise, you'd still have the cache tag. But I'd suggest you to cache the whole include (this is a perfect example for caching being really useful). Additionally setting the limit to '1' will probably help to keep the query count low if the cache is cleared.
{% cache for 3 years %}

    {% set pages = craft.entries.section('pagesSectionHandle').level(1) %}

    {% for page in pages %}

        {% set params = {
            relatedTo: { sourceElement: page, field: "entriesFieldHandle" },
            limit: '1'
        } %}

        {% set linkedEntry = craft.entries(params).first() %}

        {# Get the title from the structure (why set this twice?) #}
        {{ page.title }}

        {# Get the title from the entry #}
        {{ linkedEntry.title }}

    {% endfor %}

{% endcache %}

Structure containing "all the content"
What I'd suggest you to do is to not only use the structure as a "sorting only" structure, but to also add content directly to it.
Use entry types for your singles, if you need to load individual templates for them, and further entry types for your channel index pages. If it's thought-out carefully (modular site design) you probably won't end up with lots of entry types.
Combining those two
If you have a need to link to specific channel entries from your navigation, you could make an entry type in your structure specifically for those links to channel entries. All other content could still be added directly to the structure itself as described above.
I think that being able to edit the content directly in the pages structure is a very convenient and less geeky like experience for the client.
Edit:
This is what Naboovalley and I came up with in the chat (we should have done this before spamming the comments section). It "will give you a fully dynamic menu for your client linking between Singles, Channels and even other Structure entries":
{% cache globally for 3 years %}
{% set entries = craft.entries.section('navigation') %}


    {% nav entry in entries %}
        

            {# Check for entry type / get related entry #}
            {% if entry.type == 'linkedChannelEntry' %}
                {% set linkedEntry = entry.entriesField.limit(1).first() %}
            {% endif %}

            {% if linkedEntry %}

                {# Link to a linked channel entry #}
                {{ linkedEntry.title }}

            {% else %}

                {# Link to a structure entry #}
                {{ entry.title }}

            {% endif %}

            {# Repeat this for child entries #}
            {% ifchildren %}
                {% children %}
            {% endifchildren %}

        
    {% endnav %}

{% endcache %}

> **Comments:**
> **username_0**: I would only be interested in the page.link as the relation, no content. It's basically using a Structure to make a navigation tree of slugs to all different sections. Let's say my client want to have a navigation where he/she links to 3 singles and 10 entries from different channels.  
> **username_1**: @naboovalley ok. I changed my answer a bit and added example code to better see if I got you right.  
> **username_0**: Yeah that's basically what I was looking for, but with the exception of not using the "level(1)" since I want the client to be able to build a <ul><li></li></ul> with as many depths as it might like. I tried your code and as you'd expected it's not really optimal since it adds queries for each related linked field. You could say I'm looking for the same functionality as "Taxonomy" for EE - "one navigation to rule them all". I'm guessing a Structure isn't the best way to achieve this.  
> **username_0**: New idea! How about I build a Plugin that let's me add a URL/slug link to another entry and each time it's fetched fetches the entire current Structure links and cache them during page load to only make it use 1 query for each Structure loop. Is there any easy way for me to build the <ul><li></li></ul> list without using the {% nav %} tag for a Structure?  
> **username_1**: I wouldn't overcomplicate this, @naboovalley. As I wrote, I'd definitely add all the content (all what you'd normally put in singles) in the structure itself, then I'd make a `_navigation.html` template as an include and apply the `cache` tag to the whole navigation loop. Try it without caching (with the params I pointed out), maybe it is not that query heavy as you think. But if it is, this will only happen once (because of the 3yrs caching time) after you did an edit to the navigation!  
> **username_0**: But 3 years caching.... The client might change this Structure 10 times a day and want to check the output directly after. Also don't you find it "wierd" to have the client add "News entries" to a Structure called "Navigation"? (just an example) :)  
> **username_1**: It gets updated automatically as soon as your template would change!  
> **username_1**: @naboovalley I have no experience with EE, but I've set up a comparable navigation structure and I'd say that a structure is definitely a really great way to achieve what you want.  
> **username_1**: @naboovalley is "News entries" a channel or a single? Channels entries would still be their own sections (in the CP) but you could add the channels' index page to the structure. And why would you name the structure "Navigation", I'd say "Pages" is more appropriate if all the pages (besides channel entries) with all the content live in there ;)  
> **username_0**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/16044/discussion-between-naboovalley-and-christian-seelbach).  
> **username_1**: @naboo, I added the `limit(1)` parameter and a conditional for the `entry.type`. This should further reduce your potential queries!  

---

### Answer by username_0 on 2014-07-30. Score: 5

If you want to be able to add a class="active" to the selected URL you can use Twig's replace filter. Since the code will be cached you can't do this at runtime.
{% set navigation %}
{% cache globally for 3 years %}
{% set entries = craft.entries.section('navigation') %}


{% nav entry in entries %}
    

        {# Check for entry type / get related entry #}
        {% if entry.type == 'linkedChannelEntry' %}
            {% set linkedEntry = entry.entriesField.limit(1).first() %}
        {% endif %}

        {% if linkedEntry %}

            {# Link to a linked channel entry #}
            {{ linkedEntry.title }}

        {% else %}

            {# Link to a structure entry #}
            {{ entry.title }}

        {% endif %}

        {# Repeat this for child entries #}
        {% ifchildren %}
            {% children %}
        {% endifchildren %}

    
{% endnav %}

{% endcache %}
{% endset %}

{{ navigation|replace({('data-url="' ~ craft.request.getUrl() ~ '"'): 'class="active"'})|raw }}

> **Comments:**
> **username_1**: Really clever idea! You could also do a separate Q/A only for this particular problem (simplified or better: completely generalized), naboo. It's probably somewhat hidden in here and could come handy in many situations where caching is required, not only for adding a class to a navigation. Good work!  

---

