Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T18:02:20.883Z
Answers.Text: 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.
Answers.UserDisplayName: KSPR
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T18:14:40.850Z
Answers.Text: 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!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T18:24:40.067Z
Answers.Text: That returns `Unexpected token "name" of value "segment" ("end of print statement" expected)` on the line of the if statement.
Answers.UserDisplayName: KSPR
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T18:42:59.017Z
Answers.Text: Remove everything after the set lines and use the print statement to get the string values you need to compare.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T18:58:02.407Z
Answers.Text: I'm getting closer. `{{ activeClass ? activeClass }}` only works inside the if statement. When outside I get `Variable "activeClass" does not exist`
Answers.UserDisplayName: KSPR
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T19:15:16.447Z
Answers.Text: 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!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-13T08:20:48.717Z
Answers.LastActivityDate: 2014-08-13T08:20:48.717Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <navigation><conditional>
Title: Set parent nav to active
ThreadText: # 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') %} 
  <nav>
    <ul>
      {% for entry in navi %}
        <li><a href="{{ entry.url }}" {% if entry.url == craft.request.geturl %}class="active"{% endif %}>{{ entry.naviTitel }}</a></li>
      {% endfor %}
    </ul>
  </nav>
  
  ```
  
  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 %}`](http://buildwithcraft.com/docs/templating/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 %}
  
      <li>
          {# If the `activeClass` var is set, add its value to the link's classes #}
          <a href="{{ naviItem.url }}" class="defaultClasses{{ activeClass ? activeClass }}">
              {{ naviItem.naviTitel }}
          </a>
      </li>
  
  {% 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!  
  
  ---
  
  