如何检查FreeMarker模板中是否存在变量? - How to check if a variable exists in a FreeMarker template?

问题:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章