<element> _has_next是一个传统的Freemarker语法吗?

时间:2016-06-21 20:20:40

标签: freemarker

在freemarker文档中找到以下行,以检查是否有更多元素:

&#13;
&#13;
<#list myList as myVar>...<#if myVar_has_next>...</#if></#list>
&#13;
&#13;
&#13;

我知道&#34;?&#34;用于调用内置函数,以及关于&#34; has_next&#34;,即myVar?has_next。我不熟悉在var和内置函数之间使用下划线。这是遗留语法吗?

1 个答案:

答案 0 :(得分:0)

这似乎是一个未记录的特征。 freemarker.core.IteratorBlock中的相关代码显示myVar_has_next实际上只是变量的名称。但是因为代码检查附加到循环变量名称(_has_next)的_index(和myVar,fwiw)的出现,Freemarker能够为此变量赋值。

至少使用Freemarker 2.3.23,我无法重现你提到的错误。以下代码可以正常工作,尽管它使用myVar?has_next

public static void main(String[] args) throws IOException, TemplateException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

    Template template = new Template("test", "<#list myList as myVar>${myVar} <#if myVar?has_next>has next. </#if></#list>", cfg);
    template.process((Object) Collections.singletonMap("myList", Arrays.asList("a", "b", "c")), new OutputStreamWriter(System.out));
}

可生产

a has next. b has next. c 

修改:v2.3.23中的此功能has been deprecated(请参阅@ ddekany的评论)