Andrew Swan
Andrew Swan

Reputation: 13627

How to check for an empty Map in a Soy template?

I've read the docs for Google Soy/Closure templates, but can't find any way to check whether a Map is empty; I can only look up the map's value for a given key. Is there a way to find out the size of a Map?

My current workaround is to replace any empty maps with null and then check for null in the template, e.g.:

{if $myMap}
    <!-- Do something that requires a non-empty map -->
{/if}

Upvotes: 4

Views: 2509

Answers (1)

skaffman
skaffman

Reputation: 403501

You can get the keys of the map using the keys function, and then use length on that, so this should work:

{if length(keys($myMap)) == 0}
   ...
{/if}

Upvotes: 7

Related Questions