使用< #include>重新出错:无法找到资源

时间:2017-12-27 10:42:38

标签: freemarker

我在将模板存储在数据库中的环境中实现了freemarker代码。 例如

${bundle.key} 

将使用row_id =' key'显示行的值。 但是,当我使用include指令时,某些东西不起作用。 我有一个关键GenF的模板如下

<#function PriceFormat Number>
 <#return Number?string['0.0000']>
</#function>

如果我跑

   ${GenF.PriceFormat(1.568)}

我得到了输出

   1.5680

正如所料。 但是当我跑步时

<#include bundle.GenF>
${PriceFormat(1.568)}

我收到一条错误消息:

Can't find resource for bundle ...structures.shared.localization.bl.MultiResourceBundle, key 

我是否使用了include指令错误,或者我们的程序员在数据模型中没有正确定义?

1 个答案:

答案 0 :(得分:0)

#include需要名称 (模板的路径,“文件”名称),而不是模板内容本身。请参阅:https://freemarker.apache.org/docs/ref_directive_include.html

您似乎想要的是<@bundle.GenF?interpret />。虽然请注意,解析后的模板不会以这种方式缓存,这与使用#include调用模板时不同。要让#include能够将“bundle.GenF”解析为模板名称(或者更像是"bundle:/GenF",但这取决于您),您必须使用自定义TemplateLoader(请参阅Configuration.setTemplateLoader)。

至于您只需要这个来定义自定义数字格式,您可能还需要考虑使用自定义数字格式(https://freemarker.apache.org/docs/pgui_config_custom_formats.html),例如${1.538?string.@bundle_GenF}