Answers.Body: <p>Firstly, <a href="https://craftcms.com/docs/routing" rel="nofollow noreferrer">from the Craft docs on routing</a>:</p>
  
  <blockquote>
    <p>If the URI matches an entry’s or category’s URI, the
    section’s/category group’s template will get loaded, and the matched
    element will be made available to the template via a pre-populated
    <code>entry</code> or <code>category</code> variable.</p>
  </blockquote>
  
  <p>This means if the page you are viewing relates to an entry, Craft will have already created a variable called <code>entry</code>, which will contain the <code>EntryModel</code> for that entry, so it’s best to avoid re-assigning the <code>entry</code> variable in a template used for an entry to avoid confusion.</p>
  
  <p>To address your question, you need to check if the current URL matches the URL of an entry in the loop, which you can do by checking against <code>[craft.request.url][2]</code> like this:</p>
  
  <pre><code>{% for catEntry in craft.entries.section('category_name').limit(4) %}
      {% if craft.request.url == catEntry.url %}
        &lt;p class="active"&gt;{{ catEntry.title }}&lt;/p&gt;
      {% else %}
        &lt;p&gt;{{ catEntry.title }}&lt;/p&gt;
      {% endif %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-14T08:51:19.417Z
Answers.LastActivityDate: 2017-02-14T08:51:19.417Z
Answers.UserDisplayName: Steve Rowling
Answers.Body: <p>As Steve mentioned in Craft's docs, if the current page URI matches an entry's URI (defined in Settings > Sections), you'll have an <code>entry</code> variable available to use in your template. It's an <a href="https://craftcms.com/docs/templating/entrymodel" rel="nofollow noreferrer">EntryModel</a> of the current entry. </p>
  
  <p>Instead of testing against a URL segment, I would test your list loop against this entry variable, preferably it's <code>id</code> since that will always available and unique. Call you loop entries something else like <code>item</code> so you can test the current loop's <code>item.id</code> to the current page's <code>entry.id</code>.</p>
  
  <pre><code>{# Fetch all of the entries related to this category #}
  {% set categoryEntries = craft.entries.relatedTo(category) %}
  
  {% for item in categoryEntries %}
    {% if item.id == entry.id %}
      &lt;p class="active"&gt;{{ item.title }}&lt;/p&gt;
    {% else %}
      &lt;p&gt;{{ item.title }}&lt;/p&gt;
    {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>Or if you want to get fancier, you can use Twig's conditional shorthand (<a href="http://twig.sensiolabs.org/doc/2.x/templates.html#other-operators" rel="nofollow noreferrer">ternary operator</a>) inside a Twig output tag to simplify your code.</p>
  
  <pre><code>{% for item in categoryEntries %}
    &lt;p {{- item.id == entry.id ? ' class="active""' }}&gt;{{ item.title }}&lt;/p&gt;
  {% endfor %}
  </code></pre>
  
  <p>It's similar to this answer on the question: <a href="https://craftcms.stackexchange.com/a/561/273">How can I add a dynamic “active” CSS class to the navigation on any given page?</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-14T19:52:37.777Z
Answers.LastActivityDate: 2017-02-14T19:52:37.777Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Alex Roper
Body: <p>I'm super new to both Craft and Twig so, apologies in advance!!</p>
  
  <p>I'm looking to determine if the current entry I'm on is in a loop being called on that page.</p>
  
  <p>I have loop that goes through the entries of a category and displays them. If I'm currently viewing an entry that also appears in the loop, I would like to add an "active" class to the matching entry in the loop. </p>
  
  <p>I have been trying to possibly match the URL segment with an answer like the one found <a href="https://craftcms.stackexchange.com/questions/554/how-can-i-add-a-dynamic-active-css-class-to-the-navigation-on-any-given-page/572#572">here</a> but I need to the segment name to by dynamic. </p>
  
  <p>Currently I have:</p>
  
  <pre><code>{% for entry in craft.entries.section('category_name').limit(4) %}
        &lt;p&gt;{{ entry.title }}&lt;/p&gt;
  {% endfor %}
  </code></pre>
  
  <p>And would like it to be like:</p>
  
  <pre><code> {% for entry in craft.entries.section('category_name').limit(4) %}
      IF ENTRY IN LOOP MATCHES CURRENT ENTRY PAGE I'M ON.. ADD CLASS
        &lt;p class="active"&gt;{{ entry.title }}&lt;/p&gt;
      ELSE
        &lt;p&gt;{{ entry.title }}&lt;/p&gt;
      END IF
  {% endfor %}
  </code></pre>
  
Tags: <entries><conditional><loop>
Title: Check if current entry is in loop and add class
ThreadText: # Check if current entry is in loop and add class
  
  (Asked by: username_0 on 2017-02-13)
  
  I'm super new to both Craft and Twig so, apologies in advance!!
  
  I'm looking to determine if the current entry I'm on is in a loop being called on that page.
  
  I have loop that goes through the entries of a category and displays them. If I'm currently viewing an entry that also appears in the loop, I would like to add an "active" class to the matching entry in the loop.
  
  I have been trying to possibly match the URL segment with an answer like the one found [here](https://craftcms.stackexchange.com/questions/554/how-can-i-add-a-dynamic-active-css-class-to-the-navigation-on-any-given-page/572#572) but I need to the segment name to by dynamic.
  
  Currently I have:
  
  ```
  {% for entry in craft.entries.section('category_name').limit(4) %}
        <p>{{ entry.title }}</p>
  {% endfor %}
  
  ```
  
  And would like it to be like:
  
  ```
   {% for entry in craft.entries.section('category_name').limit(4) %}
      IF ENTRY IN LOOP MATCHES CURRENT ENTRY PAGE I'M ON.. ADD CLASS
        <p class="active">{{ entry.title }}</p>
      ELSE
        <p>{{ entry.title }}</p>
      END IF
  {% endfor %}
  
  ```
  
  **Tags:** entries, conditional, loop
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-02-14. Score: 3 | Accepted answer
  
  Firstly, [from the Craft docs on routing](https://craftcms.com/docs/routing):
  
  > If the URI matches an entry’s or category’s URI, the
  > section’s/category group’s template will get loaded, and the matched
  > element will be made available to the template via a pre-populated
  > `entry` or `category` variable.
  
  This means if the page you are viewing relates to an entry, Craft will have already created a variable called `entry`, which will contain the `EntryModel` for that entry, so it’s best to avoid re-assigning the `entry` variable in a template used for an entry to avoid confusion.
  
  To address your question, you need to check if the current URL matches the URL of an entry in the loop, which you can do by checking against `[craft.request.url][2]` like this:
  
  ```
  {% for catEntry in craft.entries.section('category_name').limit(4) %}
      {% if craft.request.url == catEntry.url %}
        <p class="active">{{ catEntry.title }}</p>
      {% else %}
        <p>{{ catEntry.title }}</p>
      {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2017-02-14. Score: 1
  
  As username_1 mentioned in Craft's docs, if the current page URI matches an entry's URI (defined in Settings > Sections), you'll have an `entry` variable available to use in your template. It's an [EntryModel](https://craftcms.com/docs/templating/entrymodel) of the current entry.
  
  Instead of testing against a URL segment, I would test your list loop against this entry variable, preferably it's `id` since that will always available and unique. Call you loop entries something else like `item` so you can test the current loop's `item.id` to the current page's `entry.id`.
  
  ```
  {# Fetch all of the entries related to this category #}
  {% set categoryEntries = craft.entries.relatedTo(category) %}
  
  {% for item in categoryEntries %}
    {% if item.id == entry.id %}
      <p class="active">{{ item.title }}</p>
    {% else %}
      <p>{{ item.title }}</p>
    {% endif %}
  {% endfor %}
  
  ```
  
  Or if you want to get fancier, you can use Twig's conditional shorthand ([ternary operator](http://twig.sensiolabs.org/doc/2.x/templates.html#other-operators)) inside a Twig output tag to simplify your code.
  
  ```
  {% for item in categoryEntries %}
    <p {{- item.id == entry.id ? ' class="active""' }}>{{ item.title }}</p>
  {% endfor %}
  
  ```
  
  It's similar to this answer on the question: [How can I add a dynamic “active” CSS class to the navigation on any given page?](https://craftcms.stackexchange.com/a/561/273)
  
  
  ---
  
  