On this page
将输出格式与模板相关联
与模板相关联的输出格式决定了该模板中是否使用以及如何使用自动转义(除非模板用指令覆盖)。默认情况下,模板具有关联的“未定义”输出格式,该格式不会转义,并且通常会给出您期望的模板引擎行为,而不关心输出格式和转义。但是,如果recognize_standard_file_extensions
setting是true
(这是incompatible_improvements 设置设置为 2.3.24 或更高版本的默认设置),则源名称以".ftlh"
结尾的模板将获得“ HTML”输出格式,而具有".ftlx"
的模板将获得“ XML”输出格式。建议使用ftlh
和ftlx
文件 extensions 来激活 HTML 和 XML 自动转义。您还可以使用template_configurations setting将基于任意名称模式的输出格式与模板相关联;请参阅以下示例。
还有另一个相关的设置,称为auto_escaping_policy
,即使当前输出格式支持也可以禁用自动转义,或者即使默认格式不转义也可以启用自动转义(但它支持)。极不建议使用此设置,因为这可能会使模板作者感到困惑。 (相反,可以使用ftl directive的auto_esc
参数或noautoesc和autoesc directive s 在模板内显式打开/关闭转义。)
要检查您是否正确配置了 FreeMarker,可以使用以下模板:
<p>Output format: ${.output_format}
<p>Auto-escaping: ${.auto_esc?c}
Configuration examples:
要启用与
*.ftlh
和*.ftlx
的自动输出格式关联,请执行以下任一操作:使用
incompatible_improvements
2.3.24 或更高版本;见如何设置 incompatible_improvements- 或者,启用显式识别的标准文件 extensions:
// Where you initalize the Configuration singletion, add:
cfg.setRecognizeStandardFileExtensions(true);
或者,如果您使用 Java *.properties
文件配置 FreeMarker:
recognizeStandardFileExtensions = true
- 假设您要将
mail
目录中的所有模板与 HTML 输出格式相关联。您可以这样实现(假设您使用cfg.getTemplate(...)
获取模板,而不是自己实例化模板):
// Where you initalize the Configuration singletion, add:
TemplateConfiguration tcHTML = new TemplateConfiguration();
tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
cfg.setTemplateConfigurations(
new ConditionalTemplateConfigurationFactory(
new PathGlobMatcher("mail/**"),
tcHTML));
或者如果您是从 Java *.properties
文件配置 FreeMarker(仅 Java 属性文件格式需要\
-s):
templateConfigurations = \
ConditionalTemplateConfigurationFactory( \
PathGlobMatcher("mail/**"), \
TemplateConfiguration(outputFormat = HTMLOutputFormat()))
- 假设您想将模板的
xml
文件 extensions 与 XML 输出格式关联,将模板的html
和htm
extensions 与 HTML 输出格式关联,并将模板的rtf
extensions 与RTF
输出格式关联。您可以这样实现(假设您使用cfg.getTemplate(...)
获取模板,而不是自己实例化它们):
TemplateConfiguration tcHTML = new TemplateConfiguration();
tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE);
TemplateConfiguration tcXML = new TemplateConfiguration();
tcXML.setOutputFormat(XMLOutputFormat.INSTANCE);
TemplateConfiguration tcRTF = new TemplateConfiguration();
tcRTF.setOutputFormat(RTFOutputFormat.INSTANCE);
cfg.setTemplateConfigurations(
new FirstMatchTemplateConfigurationFactory(
new ConditionalTemplateConfigurationFactory(
new FileExtensionMatcher("xml"),
tcXML),
new ConditionalTemplateConfigurationFactory(
new OrMatcher(
new FileExtensionMatcher("html"),
new FileExtensionMatcher("htm")),
tcHTML),
new ConditionalTemplateConfigurationFactory(
new FileExtensionMatcher("rtf"),
tcRTF)
).allowNoMatch(true)
);
或者如果您是从 Java *.properties
文件配置 FreeMarker(仅 Java 属性文件格式需要\
-s):
templateConfigurations = \
FirstMatchTemplateConfigurationFactory( \
ConditionalTemplateConfigurationFactory( \
FileExtensionMatcher("xml"), \
TemplateConfiguration(outputFormat = XMLOutputFormat())), \
ConditionalTemplateConfigurationFactory( \
OrMatcher( \
FileExtensionMatcher("html"), \
FileExtensionMatcher("htm")), \
TemplateConfiguration(outputFormat = HTMLOutputFormat())), \
ConditionalTemplateConfigurationFactory( \
FileExtensionMatcher("rtf"), \
TemplateConfiguration(outputFormat = RTFOutputFormat())), \
allowNoMatch = true)
(您可以找到一些更复杂的template_configurations
设置here...)