<p>Yeh this a common problem I've run into as well.</p>
<p>The issue is the <code>entry</code> variable only gets set when Craft is routing to an entry that's created in the CMS. Since you're <a href="https://craftcms.com/docs/3.x/routing.html" rel="nofollow noreferrer">routing</a> directly to a template, Craft can't populate the variable.</p>
<p>I can think of a couple workarounds:</p>
<p>If your search form page is defined in the CMS, just grab that (or another page):</p>
<pre><code>{% set entry = craft.entries.section('whatever').slug('search').one() %}

</code></pre>
<p>Another option is just to modify your code for fallbacks:</p>
<pre><code>{% if entry is defined %}

    {% if entry.seoPageTitle is defined and entry.seoPageTitle != null %}
      {% set metaTitle = entry.seoPageTitle %}
    {% else %}
      {% set metaTitle = entry.title %}
    {% endif %}

{% else %}

    {# set the fallback from a global for example #}
       {% set metaTitle = defaults.metaTitle %}

{% endif %}
</code></pre>
<p>That's a lot of code for basically setting variables. What you could do to help clean this up is to use the <a href="https://straightupcraft.com/events/twigs-null-coalescing-operator" rel="nofollow noreferrer">null-coa­lesc­ing operator</a> or <a href="https://twig.symfony.com/doc/3.x/filters/default.html" rel="nofollow noreferrer"><code>default</code> filter</a> in Twig:</p>
<pre><code>
{% if entry is defined %}
      {% set metaTitle = entry.seoPageTitle|default(entry.title) %}
{% else %}
      {# set the fallback from a global called &quot;defaults&quot; as an example #}
      {% set metaTitle = defaults.metaTitle %}
{% endif %}

</code></pre>

CC BY-SA 4.0
2020-12-22T08:52:00.833Z
Thank you so much! You made my day :)
I tried the "Another option" and it works now. 
Now I try to clean it up with your third proposal.
ChuckLean
CC BY-SA 4.0
2020-12-23T15:28:42.530Z
@ChuckLean You bet. If it worked for you, feel free mark it the accepted answer.
RitterKnight
CC BY-SA 4.0
2020-12-21T16:43:08.067Z
2020-12-21T16:43:08.067Z
RitterKnight
<p>I created a search form in my navigation and as it is described in the documentation, I created an search/results.html. And actually it works so far.</p>
<p>Now comes the problem: The results.html extends the &quot;_base.html&quot; and in this file I set the meta properties like this:</p>
<pre><code>{% if entry.seoPageTitle is defined and entry.seoPageTitle != null %}
  {% set metaTitle = entry.seoPageTitle %}
{% else %}
  {% set metaTitle = entry.title %}
{% endif %}
</code></pre>
<p>and the HTML:</p>
<pre><code>&lt;title&gt;{{ metaTitle }}&lt;/title&gt;
</code></pre>
<p>And also this works so far.</p>
<p>But in combination it doesn't work. When I tipe in a Word in my Searchform and click &quot;Search&quot;, then it gets &quot;Variable &quot;entry&quot; does not exist&quot;. How can I fix it?</p>

<search><entry><form>
Why the entry-Variable doesn't exists after search?
# Why the entry-Variable doesn't exists after search?

(Asked by: username_0 on 2020-12-21)

I created a search form in my navigation and as it is described in the documentation, I created an search/results.html. And actually it works so far.
Now comes the problem: The results.html extends the "_base.html" and in this file I set the meta properties like this:
{% if entry.seoPageTitle is defined and entry.seoPageTitle != null %}
  {% set metaTitle = entry.seoPageTitle %}
{% else %}
  {% set metaTitle = entry.title %}
{% endif %}

and the HTML:
{{ metaTitle }}

And also this works so far.
But in combination it doesn't work. When I tipe in a Word in my Searchform and click "Search", then it gets "Variable "entry" does not exist". How can I fix it?

**Tags:** search, entry, form

---

## 1 answers

### Answer by username_1 on 2020-12-21. Score: 1 | Accepted answer

Yeh this a common problem I've run into as well.
The issue is the entry variable only gets set when Craft is routing to an entry that's created in the CMS. Since you're routing directly to a template, Craft can't populate the variable.
I can think of a couple workarounds:
If your search form page is defined in the CMS, just grab that (or another page):
{% set entry = craft.entries.section('whatever').slug('search').one() %}


Another option is just to modify your code for fallbacks:
{% if entry is defined %}

    {% if entry.seoPageTitle is defined and entry.seoPageTitle != null %}
      {% set metaTitle = entry.seoPageTitle %}
    {% else %}
      {% set metaTitle = entry.title %}
    {% endif %}

{% else %}

    {# set the fallback from a global for example #}
       {% set metaTitle = defaults.metaTitle %}

{% endif %}

That's a lot of code for basically setting variables. What you could do to help clean this up is to use the null-coa­lesc­ing operator or default filter in Twig:

{% if entry is defined %}
      {% set metaTitle = entry.seoPageTitle|default(entry.title) %}
{% else %}
      {# set the fallback from a global called "defaults" as an example #}
      {% set metaTitle = defaults.metaTitle %}
{% endif %}

> **Comments:**
> **username_0**: Thank you so much! You made my day :) I tried the "Another option" and it works now.  Now I try to clean it up with your third proposal.  
> **username_1**: @ChuckLean You bet. If it worked for you, feel free mark it the accepted answer.  

---

