<p>This looks like a situation where you might get away with just using <a href="http://twig.sensiolabs.org/doc/filters/default.html">Twig's <code>default</code> filter</a>:</p>

<pre><code>{{ entry.metaTitle|default(title|default(siteName)) }}
</code></pre>

<p>It works even with undefined or empty strings, and methods that don't exist on objects that might not be defined themselves.</p>

CC BY-SA 3.0
2015-04-02T08:44:26.087Z
2015-04-02T08:44:26.087Z
Mike Pepper
<p>From what I can tell, the problem is in the first line of the conditional, <code>{% if entry.metaTitle is defined %}</code>.</p>

<p>If you are on an entry, <code>entry.metaTitle</code> will be defined as part of the entry model (even if it is empty), so your conditional returns true. </p>

<p>Give this a try: </p>

<pre><code>{% if (entry.metaTitle is defined) and (entry.metaTitle != '')  %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-04-02T03:11:14.287Z
no dice :( updating my question
Mark Busnelli Jr
CC BY-SA 3.0
2015-04-02T03:17:38.020Z
Do you have any errors coming through in dev mode?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-02T03:20:38.027Z
I gave it a try on one of my sites and my answer seems to work! Make sure you are doing `entry.metaTitle != ''` and not `entry.metaTitle is empty`, as that seems to make a difference.
Aaron Berkowitz
CC BY-SA 3.0
2015-04-02T03:28:30.240Z
nope :( updated my Q again :) nothing in dev mode re: errors.
Mark Busnelli Jr
CC BY-SA 3.0
2015-04-02T03:32:58.313Z
Fixed my typo :) how are you setting `title`?
Aaron Berkowitz
CC BY-SA 3.0
2015-04-02T03:35:25.577Z
On my index page using `{% extends '_inc/_layout' %}{% set title = entry.heading %}`
Mark Busnelli Jr
CC BY-SA 3.0
2015-04-02T03:49:11.383Z
What happens if you do a test when `title` has no value: `{{ title is defined ? 'defined' : 'not defined' }}`
Brad Bell
CC BY-SA 3.0
2015-04-02T03:07:51.790Z
2015-04-02T03:32:07.637Z
2015-04-02T03:32:07.637Z
Aaron Berkowitz
<p>I think this is where the problem is...</p>

<pre><code>...
{% elseif title is defined %}
    {{ title }}
...
</code></pre>

<p>I'd be willing to wager that <strong><code>title</code> is always defined</strong>. Remember, <a href="https://craftcms.stackexchange.com/a/360/45">there is a difference between being defined vs. being empty</a>.</p>

<p>Assuming your title is defined, but set to <code>null</code> or an empty string, you'll still be triggering this part of the if/else statement. But when you render the <code>{{ title }}</code> value, it will render as an empty string.</p>

<p>As a solution, try writing your second conditional like this:</p>

<pre><code>...
{% elseif (title is defined) and (title is not empty) %}
    {{ title }}
...
</code></pre>

CC BY-SA 3.0
2015-04-02T05:47:22.117Z
2015-04-02T05:47:22.117Z
2017-04-13T12:51:00.303Z
Lindsey D
<p>I'm not sure what's wrong with this original markup:</p>

<p><strong>Conditional One (doesn't work)</strong></p>

<pre><code>{% if entry.metaTitle is defined %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}
</code></pre>

<p>This works but seems a bit much for some simple checks:</p>

<p><strong>Conditional Two (does work)</strong></p>

<pre><code>{% if entry.metaTitle is defined %}
  {{ entry.metaTitle }}
{% endif %}
{% if (title is defined) and (entry.metaTitle is empty) %}
  {{ title }}
{% endif %}
{% if (title is empty) and (entry.metaTitle is empty) %}
  {{ siteName }}
{% endif %}
</code></pre>

<p>As you can see I'm checking first if <code>metaTitle</code> is set, then <code>title</code> then finally fallback to <code>siteName</code>. Entering data for the first two return results, but not sure why if <code>metaTitle</code> and <code>title</code> are empty why <code>siteName</code> doesn't return in the first conditional</p>

<h2>#</h2>

<p><strong>Update based on <a href="https://craftcms.stackexchange.com/questions/8978/conditional-parsing-issues/8979#8979">Aaron's answer</a>:</strong></p>

<p>Tried this too:</p>

<pre><code>{% if (entry.metaTitle is defined) and (entry.metaTitle is empty)  %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}
</code></pre>

<p>#</p>

<p>Also tried this, Aaron was missing a <code>(</code> in his answer :)</p>

<pre><code>{% if (entry.metaTitle is defined) and (entry.metaTitle != '')  %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}
</code></pre>

<p>But that doesn't work either with the same initial results.</p>

<conditional>
Conditional Parsing Issues
# Conditional Parsing Issues

(Asked by: username_0 on 2015-04-02)

I'm not sure what's wrong with this original markup:
Conditional One (doesn't work)
{% if entry.metaTitle is defined %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}

This works but seems a bit much for some simple checks:
Conditional Two (does work)
{% if entry.metaTitle is defined %}
  {{ entry.metaTitle }}
{% endif %}
{% if (title is defined) and (entry.metaTitle is empty) %}
  {{ title }}
{% endif %}
{% if (title is empty) and (entry.metaTitle is empty) %}
  {{ siteName }}
{% endif %}

As you can see I'm checking first if metaTitle is set, then title then finally fallback to siteName. Entering data for the first two return results, but not sure why if metaTitle and title are empty why siteName doesn't return in the first conditional
#
Update based on Aaron's answer:
Tried this too:
{% if (entry.metaTitle is defined) and (entry.metaTitle is empty)  %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}

#
Also tried this, Aaron was missing a ( in his answer :)
{% if (entry.metaTitle is defined) and (entry.metaTitle != '')  %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}

But that doesn't work either with the same initial results.

**Tags:** conditional


---

## 3 answers

### Answer by username_1 on 2015-04-02. Score: 9

This looks like a situation where you might get away with just using Twig's default filter:
{{ entry.metaTitle|default(title|default(siteName)) }}

It works even with undefined or empty strings, and methods that don't exist on objects that might not be defined themselves.


---

### Answer by username_2 on 2015-04-02. Score: 4

From what I can tell, the problem is in the first line of the conditional, {% if entry.metaTitle is defined %}.
If you are on an entry, entry.metaTitle will be defined as part of the entry model (even if it is empty), so your conditional returns true. 
Give this a try: 
{% if (entry.metaTitle is defined) and (entry.metaTitle != '')  %}
  {{ entry.metaTitle }}
{% elseif title is defined %}
  {{ title }}
{% else %}
  {{ siteName }}
{% endif %}

> **Comments:**
> **username_0**: no dice :( updating my question  
> **username_2**: Do you have any errors coming through in dev mode?  
> **username_2**: I gave it a try on one of my sites and my answer seems to work! Make sure you are doing `entry.metaTitle != ''` and not `entry.metaTitle is empty`, as that seems to make a difference.  
> **username_0**: nope :( updated my Q again :) nothing in dev mode re: errors.  
> **username_2**: Fixed my typo :) how are you setting `title`?  
> **username_0**: On my index page using `{% extends '_inc/_layout' %}{% set title = entry.heading %}`  
> **username_3**: What happens if you do a test when `title` has no value: `{{ title is defined ? 'defined' : 'not defined' }}`  

---

### Answer by username_4 on 2015-04-02. Score: 2

I think this is where the problem is...
...
{% elseif title is defined %}
    {{ title }}
...

I'd be willing to wager that title is always defined. Remember, there is a difference between being defined vs. being empty.
Assuming your title is defined, but set to null or an empty string, you'll still be triggering this part of the if/else statement. But when you render the {{ title }} value, it will render as an empty string.
As a solution, try writing your second conditional like this:
...
{% elseif (title is defined) and (title is not empty) %}
    {{ title }}
...


---

