2

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

This works in Craft 3

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

{% if url == "/" %}
   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.

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.