Java FreeMarker 模板中的 if-else

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/296619/
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:43:27  来源:igfitidea点击:

if-else in FreeMarker template

javatemplate-enginefreemarker

提问by Dónal

FreeMarker templates support an if-statement with the following syntax

FreeMarker 模板支持具有以下语法的 if 语句

<#if hot> 
  It's hot.
</#if>  

I've looked in the documentation and can't find any support for an if-else statement. Of course, I could achieve the same result with:

我查看了文档,但找不到对 if-else 语句的任何支持。当然,我可以通过以下方式获得相同的结果:

<#if hot> 
  It's hot.
</#if>  
<#if !hot> 
  It's not hot.
</#if>  

Is there support for if-else in FreeMarker?

FreeMarker 中是否支持 if-else?

采纳答案by Ulf Lindback

Yes, you can write:

是的,你可以写:

<#if hot>
it's hot
<#else>
it's not
</#if>

And if you're doing lots of freemarker, I really can recommend IntelliJ IDEA 8, its freemarker support really helps...

如果你正在做很多 freemarker,我真的可以推荐 IntelliJ IDEA 8,它的 freemarker 支持真的很有帮助......

回答by iberck

Yes, the sintaxis is:

是的,语法是:

<#if condition>

<#if condition>

...

...

<#elseif condition2>

<#elseif condition2>

...

...

<#elseif condition3>

<#elseif condition3>

...

...

<#else>

<#else>

...

...

<#/if>

<#/if>

You can find Freemarker complete reference

您可以找到Freemarker 完整参考

If you are using Netbeans, there is this plugin

如果您使用的是 Netbeans,则有此插件

回答by Vijay Dev

iberckhad already pointed out the docs. But here is the exact documentation about if-else in FreeMarker.

iberck已经指出了文档。但这里是关于FreeMarker 中 if-else的确切文档。