1

I'm using CKEditor as one of my fields, I'm trying to detect if there is any content within them, unfortunately it's still appearing even using the below code (and the field is actually empty).

{% if entry.formal is empty %}

{% else %}
<dl class="row">
  <dt class="col-sm-3">Description lists</dt>
  <dd class="col-sm-9">{{ entry.formal }}</dd>
</dl>
{% endif %}

2 Answers 2

1

At least with the 1.0.0-beta2 release of the plugin (which is using an old CKEditor 5 Developer Preview v0.11.0) build, an empty field will will output an <p> </p> tag pair.

This behavior might change once it gets updated to a stable release.

Kinda lame, but for now you could do something like this:

{% set entry = craft.entries.id(2).one() %}

{% set emptyCKEditor = "<p> </p>" %}

{% if entry.ckeditor == emptyCKEditor %}
    EMPTY
{% else %}
    NOT EMPTY
{% endif %}
1

Brad's answer should work - it makes perfect sense and it is technically correct - but for some reason it didn't for me.

Given that the goal for me was to become unstuck rather than explore the reason why, I did something that is not by any means precise, but it does the job.

I just tested against the length of the ckeditor field contents, the assumption being that, since this is a rich text field containing HTML code, it is highly unlikely (at least in my case) that the length of the string contained will be less than

<p>&nbsp;</p>

(which is what my setup was spitting out). My work-around:

{% if entry.ckeditor|length > 11 %}
  EMPTY
{% else %}
  NOT EMPTY
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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