Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T18:35:12.377Z
Answers.Text: That's great Jamie. Exactly what I needed. Thanks!
Answers.UserDisplayName: Adam Snetman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T18:41:13.877Z
Answers.Text: @AdamSnetman glad to help!
Answers.UserDisplayName: Jamie Wade
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T15:13:43.537Z
Answers.LastActivityDate: 2014-10-10T15:47:11.350Z
Answers.LastEditDate: 2014-10-10T15:47:11.350Z
Answers.UserDisplayName: Jamie Wade
Body: <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>
  
Tags: <templating><error-message><entrymodel>
Title: Template error on local development when checking entry variable
ThreadText: # 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 username_1. Exactly what I needed. Thanks!  
  > **username_1**: @username_0 glad to help!  
  
  ---
  
  