<p>You are close, instead of checking <code>entry|length</code>, you can do <code>{% if entry is defined %}</code></p>

<pre><code>{% if entry is defined and entry.sectionId == 1 %}
</code></pre>

<p>So if I am understanding correctly, the full code will be:</p>

<pre><code>{% if entry is defined and entry.sectionId == 1 %}

    //  Redirect to homepage

{% else %}

    // Do some checking into whether there's a corresponding entry, etc.

{% endif %}
</code></pre>

CC BY-SA 3.0
2014-10-10T18:35:12.377Z
That's great Jamie. Exactly what I needed. Thanks!
Adam Snetman
CC BY-SA 3.0
2014-10-10T18:41:13.877Z
@AdamSnetman glad to help!
Jamie Wade
CC BY-SA 3.0
2014-10-10T15:13:43.537Z
2014-10-10T15:47:11.350Z
2014-10-10T15:47:11.350Z
Jamie Wade
<p>I'm working on a english/spanish site that includes a language switcher link in the site footer.</p>

<p>The code for the switcher grabs the currently displayed entry, checks whether a corresponding entry exists in the other locale. If it does exist, the clicking the switcher will link to the corresponding entry. If not, you'll land on the other locale's homepage. If we're already on the homepage, just redirect to the homepage.</p>

<p>The code, simplified, looks like this:</p>

<pre><code>{% if entry.sectionId == 1 %} // The homepage

  // Redirect to homepage

{% else %}

  // Do some checking into whether there's a corresponding entry, etc.

{% endif %}
</code></pre>

<p><strong>The problem:</strong></p>

<p>I have some pages on my site that aren't entries, and on these pages, I get a template error telling me that <code>Variable "entry" does not exist</code></p>

<p>I tried rewriting this code so I'd check if that variable exists first:</p>

<pre><code>{% if entry|length %}

 // Do the previous code

{% endif %}
</code></pre>

<p>But this gives me the same error. How can I write this logic so I don't have to rely on the entry variable existing or not, which should presumably avoid the error.</p>

<p>Thanks!</p>

<templating><error-message><entrymodel>
Template error on local development when checking entry variable
# Template error on local development when checking entry variable

(Asked by: username_0 on 2014-10-10)

I'm working on a english/spanish site that includes a language switcher link in the site footer.
The code for the switcher grabs the currently displayed entry, checks whether a corresponding entry exists in the other locale. If it does exist, the clicking the switcher will link to the corresponding entry. If not, you'll land on the other locale's homepage. If we're already on the homepage, just redirect to the homepage.
The code, simplified, looks like this:
{% if entry.sectionId == 1 %} // The homepage

  // Redirect to homepage

{% else %}

  // Do some checking into whether there's a corresponding entry, etc.

{% endif %}

The problem:
I have some pages on my site that aren't entries, and on these pages, I get a template error telling me that Variable "entry" does not exist
I tried rewriting this code so I'd check if that variable exists first:
{% if entry|length %}

 // Do the previous code

{% endif %}

But this gives me the same error. How can I write this logic so I don't have to rely on the entry variable existing or not, which should presumably avoid the error.
Thanks!

**Tags:** templating, error-message, entrymodel

---

## 1 answers

### Answer by username_1 on 2014-10-10. Score: 6 | Accepted answer

You are close, instead of checking entry|length, you can do {% if entry is defined %}
{% if entry is defined and entry.sectionId == 1 %}

So if I am understanding correctly, the full code will be:
{% if entry is defined and entry.sectionId == 1 %}

    //  Redirect to homepage

{% else %}

    // Do some checking into whether there's a corresponding entry, etc.

{% endif %}

> **Comments:**
> **username_0**: That's great Jamie. Exactly what I needed. Thanks!  
> **username_1**: @AdamSnetman glad to help!  

---

