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