I'm trying to run a check within my _layout.html' to remove the navigation bar if the user is on the homepage. I've tried with the following code below, but browsing to a page which is theindex.html` of a Channel breaks the code with the following error:

Variable "entry" does not exist.

This is the code within my _layout.html

{% set handle = entry.section.handle %}
{% if handle == 'homepage' %}
{% else %}
  {% if not craft.app.user.isGuest %}
      {% set users = craft.users %}
      {% include '_navigation'%}
    {% else %}
  {% endif %}
{% endif %}

This works in Craft 3

{% set url = craft.app.request.url %}

{% if url == "/" %}
{% endif %}
  • It doesn't work if the user comes to the page via something like Facebook, which would add a /?fbclid=XXXXX parameter to the end of the URL
    – Rob Hodges
    Jul 30 '20 at 11:09
  • I've gone with: {% if not craft.app.request.segments|length %}
    – Rob Hodges
    Jul 30 '20 at 11:17

I'd rather:

{% if entry.uri == "__home__" %}
{% endif %}
  • this only works if the page is pulling from an entry, otherwise: Variable "entry" does not exist.
    – Leoncio
    Sep 7 '20 at 18:25
  • @LeoLeoncio The homepage is an entry, so if you are checking if whatever you're on is the homepage or not, you can assume false if there's no entry object. Otherwise, it's a custom setup and answering would require more info. Nov 29 '20 at 18:36

Our homepage is set via Craft's recommendation:

You can designate any one entry as a site’s homepage using a special home URI.

Since the homepage is an entry, it will always have an entry object with a uri, so I can use a simple one-liner like:

{% set isHomepage = entry is defined and entry.uri == "__home__" ? true : false %}

To prevent a Twig exception, you may wish to use entry is defined to catch this error.


Another way to detect the homepage would be looking at the request with craft.app.request, something like craft.app.request.pathInfo, if it equals /, then you've got your condition.


My site is localized, so I ended up doing...

{% if craft.app.request.url == '/' or craft.app.request.url == '/' ~ craft.app.language %}

Another option for you. Check for the presence of a url segment.

{% if not craft.app.request.segment(1) %}
    This is the homepage
{% endif %}

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.