1

I have an include setup for Navigation which works fine if I hard code in the level of the structure I want to use. However I need this to be set differently if the nav appears in the head or the footer.

head:

      {% include "_includes/nav"
        with {
          'listType' : 'list-inline pull-right',
          'navLevel'  : '2'
        }
      %}

footer:

      {% include "_includes/nav"
        with {
          'listType' : 'list-unstyled',
          'navLevel'  : '1'
        }
      %}

The include looks like this where I want the .level('<=2') to have the value passed via the include. However when I attempt this .level('<=navLevel') or similar the entire nav does not render.

{% set navigation %}

 {% set pages = craft.entries.section('navigation').level('<=2') %}

 <ul class="{{ listType }}">
   {% nav page in pages %}
     <li>
       {# Check for entry type / get related entry #}
       {% if page.type == 'internalLink' %}
           {% set linkedEntry = page.sitePageLink.limit(1).first() %}
           <a href="{{ linkedEntry.url }}"  data-url="{{ entry.url }}">{{ page.title }}</a>
       {% endif %}

       {# Check for entry type / get offsiteLinks #}
       {% if page.type == 'offsiteLink' %}
           <a href="{{ page.offSiteLink }}"  data-url="{{ entry.url }}">{{ page.title }}</a>
       {% endif %}

       {% ifchildren %}
         {# dropdown not set up yet #}
          <ul class="dropdown" style="background:pink">
              {% children %}
          </ul>
      {% endifchildren %}

     </li>
   {% endnav %}
 </ul>

 {% endset %}

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

Please advise

1 Answer 1

2

You'll need to concatenate the string you pass to the level parameter, using the ~ operator:

{% set pages = craft.entries.section('navigation').level('<='~navLevel) %}
1
  • Thanks Mats, didn't know about that. Still a noob at twig.
    – CreateSean
    Commented Nov 13, 2016 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.