问题:
I have a Freemarker template which contains a bunch of placeholders for which values are supplied when the template is processed. 我有一个Freemarker模板,其中包含一堆占位符,在处理模板时会为其提供值。 I want to conditionally include part of the template if the userName variable is supplied, something like: 如果提供了userName变量,我想有条件地包含模板的一部分,如:
[#if_exists userName]
Hi ${userName}, How are you?
[/#if_exists]
However, the FreeMarker manual seems to indicate that if_exists is deprecated, but I can't find another way to achieve this. 但是,FreeMarker手册似乎表明if_exists已被弃用,但我找不到另一种方法来实现这一点。 Of course, I could simple providing an additional boolean variable isUserName and use that like this: 当然,我可以简单地提供一个额外的布尔变量isUserName并使用如下:
[#if isUserName]
Hi ${userName}, How are you?
[/#if]
But if there's a way of checking whether userName exists then I can avoid adding this extra variable. 但是如果有一种检查userName是否存在的方法,那么我可以避免添加这个额外的变量。
解决方案:
参考一: https://en.stackoom.com/question/1HnI参考二: https://stackoom.com/question/1HnI