<p>Assuming you've configured your sections with <a href="https://craftcms.com/docs/4.x/entries.html#entry-uri-formats" rel="nofollow noreferrer">URI formats</a>, Craft will inject an <code>entry</code> variable into the global Twig context whenever the request URL matches an existing entry (this is true for the homepage or any entry with an URL).</p>
<p>This means that in cases where Craft is rendering an entry template (which is <code>{% extend %}</code>ing your layout file), there should already be an <code>entry</code> variable referencing the current entry in your Twig context and there is no need to manually query for that entry.</p>
<p>However, you might have other templates in your build that aren't entry templates, so there's a good idea to be defensive about whether or not the <code>entry</code> variable exists or not (and in cases where it doesn't, it's not an entry template and the logic is moot).</p>
<p>I don't really understand the <code>{% if entry.overlayPopup =='y' %}</code> conditional in your code; if <code>overlayPopup</code> is a Lightswitch field, it's either going to be <code>true</code>, <code>false</code> or <code>null</code>; never <code>'y'</code>.</p>
<p>But, assuming you want to include the <code>embeds/popup</code> template if a) there is a entry matching the current request URL, and b) that entry has an enabled Lightswitch toggle field with the handle <code>overlayPopup</code>, your code can probably be simplified to this:</p>
<pre><code>{% if entry is defined and entry.overlayPopup|default %}
    {% include &quot;embeds/popup&quot; %}
{% endif %}
</code></pre>
<p><em>Basically, the above code says &quot;if there is an <code>entry</code> variable defined, and if that <code>entry</code> has an attribute/field <code>overlayPopup</code>, and if that <code>overlayPopup</code> field is <code>true</code>, include the <code>embeds/popup</code> template.&quot;</em></p>
<p>If you want the opposite; the <code>embeds/popup</code> template should be included if there <em>isn't</em> an entry or if the entry <em>doesn't</em> have that Lightswitch field enabled, that'd look like this:</p>
<pre><code>{% if entry is not defined or not entry.overlayPopup|default %}
    {% include &quot;embeds/popup&quot; %}
{% endif %}
</code></pre>
<p>As a sidebar; I interpret your initial code as querying for <em>one</em> entry. Whenever you do that, you'll usually want to execute the query using <a href="https://craftcms.com/docs/4.x/element-queries.html#one" rel="nofollow noreferrer"><code>.one()</code></a> instead of <code>.all()</code>. Using <code>.one()</code>, you'll either get back a single entry (if there is one that matches your query), or a <code>null</code> variable (if there aren't any matching entries). Ie. not an array (that might be empty or not) which is always returned by <code>.all()</code> – meaning you'll avoid the redundant <code>{% for %}</code> loop.</p>
<p>Using <code>.one()</code>, your initial code could be refactored to this:</p>
<pre><code>{% set entry = craft.entries.slug(craft.app.request.segments|last).one() %}
{% if entry and entry.overlayPopup|default %}
   ...
{% endif %}
</code></pre>
<p>But again, you shouldn't have to query for the <code>entry</code> from your layout file. Most likely, it'll be in your Twig context already, whenever the current URL matches an entry.</p>

CC BY-SA 4.0
2023-11-14T22:16:51.383Z
2023-11-14T22:23:22.373Z
2023-11-14T22:23:22.373Z
Mats Mikkel Rummelhoff
<p>I'm trying to conditionally show some content inside the layout file. In my entries I have a lightswitch field - to indicate which entries should be excluded. This all works fine when I can identify the entry. But on the homepage it's not able to identify the entry - I assume because it does not have a slug. Is there a better way to do this so it work on all entries and the homepage?</p>
<pre><code>{% set last = craft.entries({ slug: craft.app.request.segments|last }) %}
{% for entry in last.all() %}
{% if entry.overlayPopup =='y' %}
  &lt;!-- NO popup --&gt;
{% else %}
  &lt;!-- SHOW popup --&gt;
  {% include &quot;embeds/popup&quot; %}
{% endif %}
{% endfor %}
</code></pre>

<conditional>
showing content conditionally in layout
# showing content conditionally in layout

(Asked by: username_0 on 2023-11-14)

I'm trying to conditionally show some content inside the layout file. In my entries I have a lightswitch field - to indicate which entries should be excluded. This all works fine when I can identify the entry. But on the homepage it's not able to identify the entry - I assume because it does not have a slug. Is there a better way to do this so it work on all entries and the homepage?
{% set last = craft.entries({ slug: craft.app.request.segments|last }) %}
{% for entry in last.all() %}
{% if entry.overlayPopup =='y' %}
  
{% else %}
  
  {% include "embeds/popup" %}
{% endif %}
{% endfor %}

**Tags:** conditional

---

## 1 answers

### Answer by username_1 on 2023-11-14. Score: 2 | Accepted answer

Assuming you've configured your sections with URI formats, Craft will inject an entry variable into the global Twig context whenever the request URL matches an existing entry (this is true for the homepage or any entry with an URL).
This means that in cases where Craft is rendering an entry template (which is {% extend %}ing your layout file), there should already be an entry variable referencing the current entry in your Twig context and there is no need to manually query for that entry.
However, you might have other templates in your build that aren't entry templates, so there's a good idea to be defensive about whether or not the entry variable exists or not (and in cases where it doesn't, it's not an entry template and the logic is moot).
I don't really understand the {% if entry.overlayPopup =='y' %} conditional in your code; if overlayPopup is a Lightswitch field, it's either going to be true, false or null; never 'y'.
But, assuming you want to include the embeds/popup template if a) there is a entry matching the current request URL, and b) that entry has an enabled Lightswitch toggle field with the handle overlayPopup, your code can probably be simplified to this:
{% if entry is defined and entry.overlayPopup|default %}
    {% include "embeds/popup" %}
{% endif %}

Basically, the above code says "if there is an entry variable defined, and if that entry has an attribute/field overlayPopup, and if that overlayPopup field is true, include the embeds/popup template."
If you want the opposite; the embeds/popup template should be included if there isn't an entry or if the entry doesn't have that Lightswitch field enabled, that'd look like this:
{% if entry is not defined or not entry.overlayPopup|default %}
    {% include "embeds/popup" %}
{% endif %}

As a sidebar; I interpret your initial code as querying for one entry. Whenever you do that, you'll usually want to execute the query using .one() instead of .all(). Using .one(), you'll either get back a single entry (if there is one that matches your query), or a null variable (if there aren't any matching entries). Ie. not an array (that might be empty or not) which is always returned by .all() – meaning you'll avoid the redundant {% for %} loop.
Using .one(), your initial code could be refactored to this:
{% set entry = craft.entries.slug(craft.app.request.segments|last).one() %}
{% if entry and entry.overlayPopup|default %}
   ...
{% endif %}

But again, you shouldn't have to query for the entry from your layout file. Most likely, it'll be in your Twig context already, whenever the current URL matches an entry.


---

