简体   繁体   English

如何在 ftl 的 <#if> 语句中使用返回 boolean 的方法

[英]How to use a method that returns boolean in ftl's <#if> statement

So, in Java, you can call some methods that return Boolean on a String , for example, string.startsWith("substring") .因此,在 Java 中,您可以调用一些在String上返回Boolean的方法,例如string.startsWith("substring") I want to use this in the ftl file, but for some reason this does not work.我想在 ftl 文件中使用它,但由于某种原因这不起作用。

<#if string.startsWith("substring")></#if>

I am pretty new to ftl, I just started learning it today, but, why this does not work?我是 ftl 的新手,我今天才开始学习它,但是,为什么这不起作用? In java, if statement accepts Boolean , startsWith method returns Boolean , so, what is the problem here?在 java 中, if语句接受BooleanstartsWith方法返回Boolean ,那么,这里有什么问题?

Error is: Expected a hash, but this has evaluated to a string .错误是: Expected a hash, but this has evaluated to a string I do not get it, how can if statement receive a hash??我不明白, if语句怎么能收到hash? Should not it receive a Boolean ?它不应该收到Boolean吗?

Most Java String methods are hidden in FTL.大多数 Java String 方法都隐藏在 FTL 中。 You have to instead use an FTL builtin.您必须改为使用内置的 FTL。 In your case, use在你的情况下,使用

<#if string?starts_with("substring")></#if>

See documentation请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM