3

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.

8

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.

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 %}
  • no dice :( updating my question – Mark Busnelli Jr Apr 2 '15 at 3:11
  • Do you have any errors coming through in dev mode? – Aaron Berkowitz Apr 2 '15 at 3:17
  • 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 Apr 2 '15 at 3:20
  • nope :( updated my Q again :) nothing in dev mode re: errors. – Mark Busnelli Jr Apr 2 '15 at 3:28
  • Fixed my typo :) how are you setting title? – Aaron Berkowitz Apr 2 '15 at 3:32
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 }}
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.