3

We have a table field that has the default value of 1 row when a new entry is published.

Unfortunately, we have

{% if entry.tableField | length %} display markup and field content... {% endif %}

and this will always be true of each entry because of the Craft default.

Obviously, we can remove the default row in the future but wondered if there was a way to extend that condition to say, for example

{% if (entry.tableField | length) and (entry.tableField.row | length) %}

2
  • What is entry.tableField.row meant to say? You want to check if the table field has at least one row and...? – Brad Bell Sep 30 '15 at 21:37
  • and has content within one of the row cells – Steven Grant Oct 1 '15 at 9:53
5

Something like this should do what you're looking for and will work for any number of rows:

{% set reallyEmpty = true %}

{% if entry.tableField | length %}
    {# Since you've got a default row set, we're guaranteed that [0] will be set #}
    {% for column in entry.tableField[0] %}
        {% if column|length %}
            {% set reallyEmpty = false %}
        {% endif %}
    {% endfor %}
{% endif %}

{% if reallyEmpty %}
    It's really empty.
{% else %}
    It's not empty.
{% endif %}
3

You can do something like this:

{% if entry.tableField|length and (
      entry.tableField[0].columnHandleOne or
      entry.tableField[0].columnHandleTwo or
      entry.tableField[0].columnHandleThree
   ) %}

If there’s only one column that you really care about being filled, you can simplify that to:

{% if entry.tableField|length and entry.tableField[0].columnHandle %}
1

For anyone googling this issue, like I did, you can also just update the Default Values in the Table fieldtype and remove the empty row that comes with that fieldtype 👍

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.