简体   繁体   中英

In Soy (Closure) templates, how can I print an expression that might be undefined at runtime?

Let's say the template model contains a map called $myMap whose keys are unknown at compile time. If my template attempts to print a map entry using {$myMap[$someValidKey]} , all is fine. However if I try to print using {$myMap[$someInvalidKey]} , the template fails to render and instead throws this error:

In 'print' tag, expression "$myMap[$someInvalidKey]" evaluates to undefined.

How can I tell the template to simply print nothing if that expression is undefined, ie no such key exists in the map?

我刚刚确认您可以有条件地包装它:

{if $myMap[$someInvalidKey]}{$myMap[$someInvalidKey]}{/if}

在未定义的情况下,可以使用{$myMap[$someInvalidKey] ?: ''}返回到不打印任何内容(空字符串)。

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