<p>You need to get all possible translations of your entry slug to do a thorough comparison.</p>

<p>This is only possible with additional DB queries (ElementCriteriaModels) for each of your navigation items and each of your installed locales, if you don't want to hardcode the values into your template.</p>

<p>Because your navigation probably doesn't change that often, I'd do it anyways and simply wrap the whole navigation in <a href="http://buildwithcraft.com/docs/templating/cache" rel="nofollow"><code>{% cache %}</code></a> tags with a really long invalidation time (it does get updated if anything is changed within the tags though!).</p>

<pre><code>{% cache for 3 years %}
{% for naviItem in navi %}

    {# Get the relevant URL segment #}
    {% set segment = craft.request.segment(1) %}

    {# Get all possible translations of the entry slug #}
    {% set enSlug = craft.entries.id(naviItem.id).locale('en').first().uri %}
    {% set frSlug = craft.entries.id(naviItem.id).locale('fr').first().uri %}
    {% set deSlug = craft.entries.id(naviItem.id).locale('de').first().uri %}

    {# Define a variable `activeClass` if the URL segment matches one of the slug translations #}
    {% if segment == enSlug or segment == frSlug or segment == deSlug %}
        {% set activeClass = ' active' %}
    {% endif %}

    &lt;li&gt;
        {# If the `activeClass` var is set, add its value to the link's classes #}
        &lt;a href="{{ naviItem.url }}" class="defaultClasses{{ activeClass ? activeClass }}"&gt;
            {{ naviItem.naviTitel }}
        &lt;/a&gt;
    &lt;/li&gt;

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

CC BY-SA 3.0
2014-08-13T18:02:20.883Z
Hmm. That doesn't work. It adds either a active class to every link or to no link. On second tought I'm not sure if its necessary to trigger the locales at all. Wouldn't it be enough to add a listener for the project_entry slug and then add active to the project link? But maybe this is all too much fuss, I guess hardcoding each navi link is just easier. But thanks anyway. The learning curve from such posts is still immense.
KSPR
CC BY-SA 3.0
2014-08-13T18:14:40.850Z
C'mon, it's too late to backpedal. I'd try s.th. like this `{{ segment ~ ' == ' ~ enSlug ~ ' == ' ~ deSlug }}??` anywhere within your loop to find the problem!
carlcs
CC BY-SA 3.0
2014-08-13T18:24:40.067Z
That returns `Unexpected token "name" of value "segment" ("end of print statement" expected)` on the line of the if statement.
KSPR
CC BY-SA 3.0
2014-08-13T18:42:59.017Z
Remove everything after the set lines and use the print statement to get the string values you need to compare.
carlcs
CC BY-SA 3.0
2014-08-13T18:58:02.407Z
I'm getting closer. `{{ activeClass ? activeClass }}` only works inside the if statement. When outside I get `Variable "activeClass" does not exist`
KSPR
CC BY-SA 3.0
2014-08-13T19:15:16.447Z
You probably modified the code and now define the var within a for loop. If you want to access a var outside a for loop you have to define it before the loop!
carlcs
CC BY-SA 3.0
2014-08-13T08:20:48.717Z
2014-08-13T08:20:48.717Z
carlcs
<p>I have a navigation like this:</p>

<pre><code>{% set navi = craft.entries.id('2, 4, 5, 6' ).order('id asc') %} 
&lt;nav&gt;
  &lt;ul&gt;
    {% for entry in navi %}
      &lt;li&gt;&lt;a href="{{ entry.url }}" {% if entry.url == craft.request.geturl %}class="active"{% endif %}&gt;{{ entry.naviTitel }}&lt;/a&gt;&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
&lt;/nav&gt;
</code></pre>

<p>This results in something like:</p>

<ul>
<li>Home</li>
<li>Projects</li>
<li>About</li>
<li>Contact</li>
</ul>

<p>Now under Projects (Wich is a Single entry) are entry pages like this:</p>

<ul>
<li>url/projects/project_entry_1</li>
<li>url/projects/project_entry_2</li>
</ul>

<p>Now what I'm trying to achieve is that when viewing a project_entry, the nav link "Project" should have the class active which is not the case right now. Is this somehow possible with this navi markup or do I have to write something like this.</p>

<p>I taught about writing a conditional {% if url_segment 2 == project %} but that doesn't work because this would give all links the class active and since I have a multi-language installation segment_2 is not always the single slug.</p>

<p>What is the prettiest way to achieve this?</p>

<p>Thank you.</p>

<navigation><conditional>
Set parent nav to active
# Set parent nav to active

(Asked by: username_0 on 2014-08-13)

I have a navigation like this:
{% set navi = craft.entries.id('2, 4, 5, 6' ).order('id asc') %} 

  
    {% for entry in navi %}
      {{ entry.naviTitel }}
    {% endfor %}
  


This results in something like:

Home
Projects
About
Contact

Now under Projects (Wich is a Single entry) are entry pages like this:

url/projects/project_entry_1
url/projects/project_entry_2

Now what I'm trying to achieve is that when viewing a project_entry, the nav link "Project" should have the class active which is not the case right now. Is this somehow possible with this navi markup or do I have to write something like this.
I taught about writing a conditional {% if url_segment 2 == project %} but that doesn't work because this would give all links the class active and since I have a multi-language installation segment_2 is not always the single slug.
What is the prettiest way to achieve this?
Thank you.

**Tags:** navigation, conditional

---

## 1 answers

### Answer by username_1 on 2014-08-13. Score: 2

You need to get all possible translations of your entry slug to do a thorough comparison.
This is only possible with additional DB queries (ElementCriteriaModels) for each of your navigation items and each of your installed locales, if you don't want to hardcode the values into your template.
Because your navigation probably doesn't change that often, I'd do it anyways and simply wrap the whole navigation in {% cache %} tags with a really long invalidation time (it does get updated if anything is changed within the tags though!).
{% cache for 3 years %}
{% for naviItem in navi %}

    {# Get the relevant URL segment #}
    {% set segment = craft.request.segment(1) %}

    {# Get all possible translations of the entry slug #}
    {% set enSlug = craft.entries.id(naviItem.id).locale('en').first().uri %}
    {% set frSlug = craft.entries.id(naviItem.id).locale('fr').first().uri %}
    {% set deSlug = craft.entries.id(naviItem.id).locale('de').first().uri %}

    {# Define a variable `activeClass` if the URL segment matches one of the slug translations #}
    {% if segment == enSlug or segment == frSlug or segment == deSlug %}
        {% set activeClass = ' active' %}
    {% endif %}

    
        {# If the `activeClass` var is set, add its value to the link's classes #}
        
            {{ naviItem.naviTitel }}
        
    

{% endfor %}
{% endcache %}

> **Comments:**
> **username_0**: Hmm. That doesn't work. It adds either a active class to every link or to no link. On second tought I'm not sure if its necessary to trigger the locales at all. Wouldn't it be enough to add a listener for the project_entry slug and then add active to the project link? But maybe this is all too much fuss, I guess hardcoding each navi link is just easier. But thanks anyway. The learning curve from such posts is still immense.  
> **username_1**: C'mon, it's too late to backpedal. I'd try s.th. like this `{{ segment ~ ' == ' ~ enSlug ~ ' == ' ~ deSlug }}??` anywhere within your loop to find the problem!  
> **username_0**: That returns `Unexpected token "name" of value "segment" ("end of print statement" expected)` on the line of the if statement.  
> **username_1**: Remove everything after the set lines and use the print statement to get the string values you need to compare.  
> **username_0**: I'm getting closer. `{{ activeClass ? activeClass }}` only works inside the if statement. When outside I get `Variable "activeClass" does not exist`  
> **username_1**: You probably modified the code and now define the var within a for loop. If you want to access a var outside a for loop you have to define it before the loop!  

---

