将输出格式与模板相关联

与模板相关联的输出格式决定了该模板中是否使用以及如何使用自动转义(除非模板用指令覆盖)。默认情况下,模板具有关联的“未定义”输出格式,该格式不会转义,并且通常会给出您期望的模板引擎行为,而不关心输出格式和转义。但是,如果recognize_standard_file_extensions settingtrue(这是incompatible_improvements 设置设置为 2.3.24 或更高版本的默认设置),则源名称以".ftlh"结尾的模板将获得“ HTML”输出格式,而具有".ftlx"的模板将获得“ XML”输出格式。建议使用ftlhftlx文件 extensions 来激活 HTML 和 XML 自动转义。您还可以使用template_configurations setting将基于任意名称模式的输出格式与模板相关联;请参阅以下示例。

还有另一个相关的设置,称为auto_escaping_policy,即使当前输出格式支持也可以禁用自动转义,或者即使默认格式不转义也可以启用自动转义(但它支持)。极不建议使用此设置,因为这可能会使模板作者感到困惑。 (相反,可以使用ftl directiveauto_esc参数或noautoescautoesc directive s 在模板内显式打开/关闭转义。)

要检查您是否正确配置了 FreeMarker,可以使用以下模板:

<p>Output format: ${.output_format}
<p>Auto-escaping: ${.auto_esc?c}

sched 义输出格式表在这里...

Configuration examples:

// Where you initalize the Configuration singletion, add:
cfg.setRecognizeStandardFileExtensions(true);

或者,如果您使用 Java *.properties文件配置 FreeMarker:

recognizeStandardFileExtensions = true
// 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()))
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...)

上一章 首页 下一章