Answers.Body: <p>Craft doesn't redirect to 404 if a page isn't found, it just loads the 404 template and keeps the invalid uri.</p>
  
  <p><strong>What currently happens:</strong></p>
  
  <ul>
  <li>example.com/invalid_uri</li>
  <li>Craft doesn't find it and shows the 404 template</li>
  <li>Your conditional checks if <code>404</code> is in the uri, but it isn't.</li>
  <li>Your code now tries to access the <code>entry</code> variable which isn't defined.</li>
  <li>This results in a Twig error.</li>
  </ul>
  
  <p>.</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>Add an <code>entry is defined</code> to your conditional:</p>
  
  <pre><code>{% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" and entry is defined %}
  ....
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T20:56:57.470Z
Answers.Text: That did it Victor thank you, for info I also needed to add `or entry is not defined` to the last 404 conditional which makes sense with what you have explained.
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T20:51:41.657Z
Answers.LastActivityDate: 2014-06-24T20:51:41.657Z
Answers.UserDisplayName: Victor
Answers.Body: <p>Take a look at what you have in your <em>_includes/_page_meta</em> file at line 4.</p>
  
  <p>Most likely you just need to wrap it in a conditional with <code>is defined</code> like</p>
  
  <pre><code>{% if entry is defined %}
      {# your template stuff here #}
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T20:52:52.430Z
Answers.Text: I think Victor's answer is better!
Answers.UserDisplayName: Alex Kendrick
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T20:58:32.380Z
Answers.Text: Thanks Alex, yes Victor's solution fixed it (I added the line numbers in to try and help to).
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-24T20:50:22.257Z
Answers.LastActivityDate: 2014-06-24T20:50:22.257Z
Answers.UserDisplayName: Alex Kendrick
Body: <p>I am trying to create a meta include to display all my meta data and I have the following set up so far:</p>
  
  <pre><code>3. {% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" %}
    4. &lt;title&gt;{% if entry.metaTitle is defined %}{{ entry.metaTitle }}{% else %}{{ entry.title }}{% endif %} | {{ siteName }}&lt;/title&gt;
    5. {% if entry.metaDescription is defined %}
    6. &lt;meta name="description" content="{{ entry.metaDescription }}"&gt;
    7. {% endif %}
  8. {% endif %}
  
  {# 404 =============== #}
  
  {% if craft.request.getSegment(1)  == "404" %}
    &lt;title&gt;Page Missing | {{ siteName }}&lt;/title&gt;
    &lt;meta name="description" content="Sorry the page you are looking for is missing, it may have been moved or deleted, please use the navigation to search for similar content."&gt;
  {% endif %}
  </code></pre>
  
  <p>If I visit main pages the code works and if I go to example.com/404 the code works, but if I visit an invalid url example.com/foo then I get a Twig Error Runtime error:</p>
  
  <p><img src="https://i.sstatic.net/cJmTb.png" alt="enter image description here"></p>
  
Tags: <templating><conditional>
Title: Twig Error Runtime When Using Conditionals
ThreadText: # Twig Error Runtime When Using Conditionals
  
  (Asked by: username_0 on 2014-06-24)
  
  I am trying to create a meta include to display all my meta data and I have the following set up so far:
  
  ```
  3. {% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" %}
    4. <title>{% if entry.metaTitle is defined %}{{ entry.metaTitle }}{% else %}{{ entry.title }}{% endif %} | {{ siteName }}</title>
    5. {% if entry.metaDescription is defined %}
    6. <meta name="description" content="{{ entry.metaDescription }}">
    7. {% endif %}
  8. {% endif %}
  
  {# 404 =============== #}
  
  {% if craft.request.getSegment(1)  == "404" %}
    <title>Page Missing | {{ siteName }}</title>
    <meta name="description" content="Sorry the page you are looking for is missing, it may have been moved or deleted, please use the navigation to search for similar content.">
  {% endif %}
  
  ```
  
  If I visit main pages the code works and if I go to example.com/404 the code works, but if I visit an invalid url example.com/foo then I get a Twig Error Runtime error:
  
  ![enter image description here](https://i.sstatic.net/cJmTb.png)
  
  **Tags:** templating, conditional
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-06-24. Score: 3 | Accepted answer
  
  Craft doesn't redirect to 404 if a page isn't found, it just loads the 404 template and keeps the invalid uri.
  
  **What currently happens:**
  
  * example.com/invalid\_uri
  * Craft doesn't find it and shows the 404 template
  * Your conditional checks if `404` is in the uri, but it isn't.
  * Your code now tries to access the `entry` variable which isn't defined.
  * This results in a Twig error.
  
  .
  
  **Solution:**
  
  Add an `entry is defined` to your conditional:
  
  ```
  {% if craft.request.getSegment(1) != "404" and craft.request.getSegment(1) != "databytes" and entry is defined %}
  ....
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_0**: That did it username_1 thank you, for info I also needed to add `or entry is not defined` to the last 404 conditional which makes sense with what you have explained.  
  
  ---
  
  ### Answer by username_2 on 2014-06-24. Score: 1
  
  Take a look at what you have in your *\_includes/\_page\_meta* file at line 4.
  
  Most likely you just need to wrap it in a conditional with `is defined` like
  
  ```
  {% if entry is defined %}
      {# your template stuff here #}
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_2**: I think username_1's answer is better!  
  > **username_0**: Thanks username_2, yes username_1's solution fixed it (I added the line numbers in to try and help to).  
  
  ---
  
  