問題:
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