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

2.4k Views Asked by At

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}
1

There are 1 best solutions below

0
On BEST ANSWER

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}