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

时间:2015-03-31 02:27:57

标签: google-closure-templates soy-templates

我已经阅读了Google Soy / Closure模板的docs,但找不到任何方法来检查地图是否为空;我只能查找给定键的地图值。有没有办法找出地图的大小?

我目前的解决方法是用null替换所有空地图,然后在模板中检查null,例如:

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

1 个答案:

答案 0 :(得分:4)

您可以使用keys函数获取地图的键,然后使用length,这样就可以了:

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