0

Matrix 2.5 and 2.6 on ExpressionEngine 2.7.3.

The :total_rows property doesn't work when there are no rows. It works fine when there are rows.

Below isn't actual code, but it's showing the gist.

{exp:channel:entries dynamic="no" channel="regions" url_title="[url_title]"}
    {if "{social_networks:total_rows}" > 0}
        {!--  Do the things --}
    {if:else}
        {!--  Don't do the things --}
    {/if}
{/exp:channel:entries}

Outputting {social_networks:total_rows} is an empty string (which I can't even match against with a conditional) until there's a Matrix row.

Has anyone else noticed this? I don't want to have to start pulling the plugin apart.

2 Answers 2

1

I believe when there are zero rows the matrix variable just returns false, so you can just use it in the conditional like this.

{exp:channel:entries dynamic="no" channel="regions" url_title="[url_title]"}
    {if social_networks}
        {!--  Do the things --}
    {if:else}
        {!--  Don't do the things --}
    {/if}
{/exp:channel:entries}
2
  • Thanks. I thought I'd tried that, but evidently not! The docs definitely imply the :total_rows property would work in this context docs.pixelandtonic.com/matrix/templating/field.html#total-rows
    – JamesNZ
    Commented Apr 8, 2015 at 2:42
  • For EE 2.9, you should go with {if '{matrix:total_rows}' && '{matrix:total_rows}' > 0 }stuff happens{/if} to avoid template errors. Commented Apr 8, 2015 at 6:38
1

{matrix:total_rows} can't work if there are no rows in the field simply because EE doesn't parse any field tags that don't have any actual data. Matrix never even gets called and the tag gets replaced with nothing.

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.