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.

How do I check if a field is blank? I thought it was something like

{% if article.articleImage is defined %}

Which in this case is testing an Assets field (and seems to work). Tried the same for a text field (in a Matrix row), and that doesn't work, as far as I can tell:

{{ if block.heading is defined }}
share|improve this question
5  
FYI, there's a nice Q&A on the difference between is defined and is not null. –  Matt Stein Jul 8 at 16:13

3 Answers 3

up vote 15 down vote accepted

The answer depends on the field type.

Assets, Categories, Checkboxes, Dropdown, Entries, Matrix, Multi-select, Radio Buttons, Tags, and Users fields each will each appear to be non-empty, even if they don’t have a user-submitted value, because in each case, entry.myFieldHandle will still return something:

  • an ElementCriteriaModel object for Assets, Categories, Entries, Matrix, Tags, and Users fields
  • a MultiOptionsFieldData object for Checkboxes and Multi-select fields
  • a SingleOptionFieldData object for Dropdown and Radio Buttons fields

So for each of those field types, you will need to use the |length filter to determine if there is an actual value.

{% if not entry.myCheckboxesField|length %}

Also worth noting for Assets, Categories, Entries, Matrix, Tags, and Users fields: ElementCriteriaModel objects also have a total() function which can be used to speed up your template if all you need to know is whether any elements were selected, and you won’t be actually looping through them further down in the template. (|length actually fetches the elements first, and then returns the count of the results, but stores those elements internally in case they are going to be looped through later on. .total() runs a separate, much less expensive DB query, but the results won’t be cached internally for later use.)

{% if not entry.myCategoriesField.total() %}

Color, Date/Time, Lightswitch, Number, Plain Text, Rich Text, and Table fields will each return an empty value (null, an empty string/array, etc.) when there is no value, so each of those field types are safe to use with the is empty test:

{% if entry.myTextField is empty %}

This would also work:

{% if not entry.myTextField %}

In all cases, the |length filter will work as expected, so if you don’t want to remember this stuff, just use that.

share|improve this answer
    
Thanks for this clarification. Helpful indeed. –  Ben Parizek Jul 13 at 0:36

If you have a peek through the Twig documentation, you'll come across the empty test.

{% if block.heading is empty %}
    {# ... #}
{% endif %}
share|improve this answer

Stuart's answer totally works. You can also use Twig's length filter:

{% if article.articleImage|length %}
share|improve this answer

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.