简体   繁体   中英

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, eg:

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

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM