5

I am trying to simply change a nav element if one homepage and another if on the blog or another page.

I can the following to display if I set the following statement to !=

{% if craft.request.getUrl() == 'http://testsite.com/page/' %}
    {{siteUrl}}
{% endif %}

However I can't figure out what that is returning as no matter what I put it doesn't work.

  • 1
    You can always see what a variable returns by outputting it like so {{ craft.request.getUrl() }} – naboovalley Oct 30 '14 at 7:28
10

Personally, I'd use segments for that. Something like:

{% if craft.request.lastSegment == "page" %}
   do something
{% endif %}

or

{% if craft.request.segments(1) == "page" %}
   do something
{% endif %}

These days, though, when not making a dynamic nav, I tend to pass a variable to my extended layouts or nav include. I find it much more flexible / robust than just relying on URL structures or segments.

In child template

{% set currentNav = "home" %}

or

{% include 'includes/_nav' with {'currentNav':'home'} %}

In include or extended layout

{% if currentNav == "home" %}
   do something
{% endif %}
6

In addition to the options Jerome mentioned, you can also test for the Entry Type, which typically would be different for homepage vs blog, etc.

{% if entry.type == 'home' %}
...
{% endif %}

Switch also works well here.

{% switch entry.type %}
   {% case 'home' %}
   ...
   {% case 'blog' %}
   ...
   {% default %}
   ...
{% endswitch %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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