7

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 %}

8 Answers 8

3

This works in Craft 3

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

{% if url == "/" %}
   homepage
{% endif %}
2
  • 1
    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, 2020 at 11:09
  • 1
    I've gone with: {% if not craft.app.request.segments|length %}
    – Rob Hodges
    Jul 30, 2020 at 11:17
8

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 %}

6

I'd rather:

{% if entry.uri == "__home__" %}
  🏠
{% endif %}
2
  • this only works if the page is pulling from an entry, otherwise: Variable "entry" does not exist.
    – Leoncio
    Sep 7, 2020 at 18:25
  • 1
    @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, 2020 at 18:36
5

If using Craft 3.3.6 or later, the best option is probably entry.isHomepage, i.e.

{% if entry is defined and entry.isHomepage %}
   This is the homepage
{% endif %}
2

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

https://twig.symfony.com/doc/2.x/tests/defined.html

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.

0
2

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

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

This works nicely and limits error possiblities:

{% if craft.app.urlManager.matchedElement and craft.app.urlManager.matchedElement.uri == '__home__' %}
    Show on homepage
{% endif %}
1

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

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

Your Answer

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

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