Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

With dev mode on, I'm getting a warning there is no entry.title, which I know about already because that's what I'm testing for:

{% if entry.title | length == 0 %}
{% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}

Is there a way to test for a variable without dev mode triggering this notice?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

If you want to set entry to a new value if entry.title is undefined or 0 length, try this:

{% if !(entry is defined and entry.title is defined and entry.title|length) %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}
share|improve this answer

I've had luck using "is defined" before using other logic operators.

This should do it:

{% if entry.title is defined and entry.title | length == 0 %}
    {% set entry = craft.entries.section('sectionname').slug(craft.request.segment(2)).first() %}
{% endif %}
share|improve this answer
    
Excellent. Thanks. –  Clive Portman Jun 26 at 9:56
2  
I think you want to set entry to the new value if entry.title is not defined or is 0 length. Which is not what this code does. –  Marion Newlevant Jun 27 at 5:36
    
You're right. I ended up using something slightly different in practice. –  Clive Portman Jun 27 at 14:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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