简体   繁体   English

如何在Soy模板中检查空地图?

[英]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; 我已经阅读了有关Google Soy / Closure模板的文档 ,但是找不到任何方法来检查地图是否为空; 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: 我当前的解决方法是用null替换所有空映射,然后在模板中检查null ,例如:

{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: 您可以使用keys函数获取地图的keys ,然后在其上使用length ,因此应该可以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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