Java 如何检查 FreeMarker 模板中是否存在变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/306732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:55:35  来源:igfitidea点击:

How to check if a variable exists in a FreeMarker template?

javatemplatesfreemarker

提问by Dónal

I have a Freemarker template which contains a bunch of placeholders for which values are supplied when the template is processed. I want to conditionally include part of the template if the userName variable is supplied, something like:

我有一个 Freemarker 模板,其中包含一堆占位符,在处理模板时为其提供值。如果提供了 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. Of course, I could simple providing an additional boolean variable isUserName and use that like this:

然而,FreeMarker 手册似乎表明 if_exists 已被弃用,但我找不到另一种方法来实现这一点。当然,我可以简单地提供一个额外的布尔变量 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 是否存在,那么我可以避免添加这个额外的变量。

采纳答案by Ulf Lindback

To check if the value exists:

检查值是否存在:

[#if userName??]
   Hi ${userName}, How are you?
[/#if]

Or with the standard freemarker syntax:

或者使用标准的 freemarker 语法:

<#if userName??>
   Hi ${userName}, How are you?
</#if>

To check if the value exists and is not empty:

要检查该值是否存在且不为空:

<#if userName?has_content>
    Hi ${userName}, How are you?
</#if>

回答by Ulf Lindback

Also I think if_existswas used like:

另外我认为if_exists 的用法如下:

Hi ${userName?if_exists}, How are you?

which will not break if userName is null, the result if null would be:

如果 userName 为空,则不会中断,如果为空,结果将是:

Hi , How are you?

if_exists is now deprecated and has been replaced with the default operator ! as in

if_exists 现在已弃用并已替换为默认运算符!如

Hi ${userName!}, How are you?

the default operator also supports a default value, such as:

default 运算符还支持默认值,例如:

Hi ${userName!"John Doe"}, How are you?

回答by user1546081

This one seems to be a better fit:

这个似乎更合适:

<#if userName?has_content>
... do something
</#if>

http://freemarker.sourceforge.net/docs/ref_builtins_expert.html

http://freemarker.sourceforge.net/docs/ref_builtins_expert.html

回答by Jake Toronto

I think a lot of people are wanting to be able to check to see if their variable is not empty as well as if it exists. I think that checking for existence and emptiness is a good idea in a lot of cases, and makes your template more robust and less prone to silly errors. In other words, if you check to make sure your variable is not null AND not empty before using it, then your template becomes more flexible, because you can throw either a null variable or an empty string into it, and it will work the same in either case.

我认为很多人都希望能够检查他们的变量是否不为空以及是否存在。我认为在很多情况下检查存在和空性是一个好主意,并且可以使您的模板更加健壮,并且不容易出现愚蠢的错误。换句话说,如果您在使用之前检查以确保您的变量不为空且不为空,那么您的模板将变得更加灵活,因为您可以向其中抛出一个空变量或一个空字符串,它的工作方式相同在任一情况下。

<#if p?? && p?has_content>1</#if>

Let's say you want to make sure that pis more than just whitespace. Then you could trim it before checking to see if it has_content.

假设您想确保这p不仅仅是空格。然后你可以在检查它之前修剪它has_content

<#if p?? && p?trim?has_content>1</#if>

UPDATE

更新

Please ignore my suggestion -- has_contentis all that is needed, as it does a null check along with the empty check. Doing p?? && p?has_contentis equivalent to p?has_content, so you may as well just use has_content.

请忽略我的建议——has_content这就是所需要的,因为它会与空检查一起进行空检查。Doingp?? && p?has_content相当于p?has_content,所以你不妨直接使用has_content.

回答by Petter Friberg

For versions previous to FreeMarker 2.3.7

对于 FreeMarker 2.3.7 之前的版本

You can not use ??to handle missing values, the old syntax is:

你不能??用来处理缺失值,旧的语法是:

<#if userName?exists>
   Hi ${userName}, How are you?
</#if>