Answers.Body: <p>Not tested, but I think you can test if it is a pagination page. So </p>
  
  <pre><code>{% if (craft.request.getSegment('2') == '') or (craft.request.getPageNum() != 1) %}active{% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-01T19:14:05.323Z
Answers.LastActivityDate: 2016-09-01T19:14:05.323Z
Answers.UserDisplayName: AbbeyDesign
Answers.Body: <p>Here is a possible macro. It uses <a href="https://craftcms.com/docs/templating/craft.request#getSegment" rel="nofollow">getSegment()</a> to get the second segment, and twig's <a href="http://twig.sensiolabs.org/doc/templates.html#comparisons" rel="nofollow">matches</a> operator to compare that segment to the page trigger. Note that there is a <a href="https://craftcms.com/docs/config-settings#pageTrigger" rel="nofollow">pageTrigger</a> setting, and this macro assumes you haven't changed that trigger.</p>
  
  <pre><code>{% macro is_active(segment) %}
    {% set firstSeg = craft.request.getSegment(1) %}
    {% set secondSeg = craft.request.getSegment(2) %}
    {# is secondSeg a pagination segment? #}
    {% set isPagination = secondSeg and secondSeg matches '/^p[0-9]+/' %}
  
    {# active: firstSeg is our segment, and no secondSeg, or secondSeg is pagination seg #}
    {% if firstSeg == 'segment' and (not secondSeg or isPagination) %}
      active
    {% endif %}
  {% endmacro %}
  </code></pre>
  
  <p>You might also want to use the <a href="https://github.com/marionnewlevant/craft-twig_perversion" rel="nofollow">twigperversion</a> plugin which provides a <code>{% return %}</code> tag:</p>
  
  <pre><code>{% macro is_active(segment) %}
    ...
    {% return firstSeg == 'segment' and (not secondSeg or isPagination) %}
  {% endmacro %}
  </code></pre>
  
  <p>and then <code>{{ macro.is_active('photography') ? 'active' }}</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-01T20:12:44.573Z
Answers.Text: @AbbeyDesign is right: [getPageNum](https://craftcms.com/docs/templating/craft.request#getPageNum) is a cleaner way to go
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-01T20:10:40.330Z
Answers.LastActivityDate: 2016-09-01T20:10:40.330Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>I'm using a macro to check segment 1 to set an active state on my navigation and all is working correctly. However I would like to change it so that if segment_2 is not empty it doesn't display the active state, with the exception of being pagination in segment-2</p>
  
  <p>so <code>domain.com/photography</code> and <code>domain.com/photography/p2</code> would get active but <code>domain.com/photography/slug</code> would not be active</p>
  
  <p>my current code is:</p>
  
  <p>macro:</p>
  
  <pre><code>{% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %}
  </code></pre>
  
  <p>and nav:</p>
  
  <pre><code>&lt;ul class="nav navbar-nav"&gt;
    &lt;li class="{{ macro.is_active('') }}"&gt;&lt;a href="/"&gt;Home&lt;/a&gt;&lt;/li&gt;
    &lt;li class="{{ macro.is_active('photography') }}"&gt;&lt;a href="/photography"&gt;All Photography&lt;/a&gt;&lt;/li&gt;
    &lt;li class="{{ macro.is_active('calendar') }}"&gt;&lt;a href="/calendar"&gt;Archives&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
Tags: <templating><navigation><macro>
Title: active state macro how to check if segment 2 is empty
ThreadText: # active state macro how to check if segment 2 is empty
  
  (Asked by: username_0 on 2016-09-01)
  
  I'm using a macro to check segment 1 to set an active state on my navigation and all is working correctly. However I would like to change it so that if segment\_2 is not empty it doesn't display the active state, with the exception of being pagination in segment-2
  
  so `domain.com/photography` and `domain.com/photography/p2` would get active but `domain.com/photography/slug` would not be active
  
  my current code is:
  
  macro:
  
  ```
  {% macro is_active(segment) %}{% if craft.request.firstSegment == segment %}active{% endif %}{% endmacro %}
  
  ```
  
  and nav:
  
  ```
  <ul class="nav navbar-nav">
    <li class="{{ macro.is_active('') }}"><a href="/">Home</a></li>
    <li class="{{ macro.is_active('photography') }}"><a href="/photography">All Photography</a></li>
    <li class="{{ macro.is_active('calendar') }}"><a href="/calendar">Archives</a></li>
  </ul>
  
  ```
  
  **Tags:** templating, navigation, macro
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-09-01. Score: 2
  
  Not tested, but I think you can test if it is a pagination page. So
  
  ```
  {% if (craft.request.getSegment('2') == '') or (craft.request.getPageNum() != 1) %}active{% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2016-09-01. Score: 1
  
  Here is a possible macro. It uses [getSegment()](https://craftcms.com/docs/templating/craft.request#getSegment) to get the second segment, and twig's [matches](http://twig.sensiolabs.org/doc/templates.html#comparisons) operator to compare that segment to the page trigger. Note that there is a [pageTrigger](https://craftcms.com/docs/config-settings#pageTrigger) setting, and this macro assumes you haven't changed that trigger.
  
  ```
  {% macro is_active(segment) %}
    {% set firstSeg = craft.request.getSegment(1) %}
    {% set secondSeg = craft.request.getSegment(2) %}
    {# is secondSeg a pagination segment? #}
    {% set isPagination = secondSeg and secondSeg matches '/^p[0-9]+/' %}
  
    {# active: firstSeg is our segment, and no secondSeg, or secondSeg is pagination seg #}
    {% if firstSeg == 'segment' and (not secondSeg or isPagination) %}
      active
    {% endif %}
  {% endmacro %}
  
  ```
  
  You might also want to use the [twigperversion](https://github.com/username_2/craft-twig_perversion) plugin which provides a `{% return %}` tag:
  
  ```
  {% macro is_active(segment) %}
    ...
    {% return firstSeg == 'segment' and (not secondSeg or isPagination) %}
  {% endmacro %}
  
  ```
  
  and then `{{ macro.is_active('photography') ? 'active' }}`
  
  > **Comments:**
  > **username_2**: @username_1 is right: [getPageNum](https://craftcms.com/docs/templating/craft.request#getPageNum) is a cleaner way to go  
  
  ---
  
  