Freemarker处理包含的模板

时间:2014-03-05 14:51:57

标签: java freemarker

我有自由人模板

<#import "includes/header.ftl" as header>

<h1>${title}</h1>
header.ftl中的

<html>
<head>
    <title>${title}</title>
</head>
<body>

我有非常简单的代码来呈现它

Configuration cfg = new Configuration();
File dir = new File("/usr/cms/data/templates");
cfg.setDirectoryForTemplateLoading(dir);
cfg.setDefaultEncoding("UTF-8");
cfg.setLocale(Locale.US);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

Map<String, Object> input = new HashMap<String, Object>();
input.put("title","Test title");

Template template = cfg.getTemplate("template.ftl");
Writer writer = new StringWriter();
template.process(input,writer);

模板已处理,但header.ftl未包含在StringWriter内的字符串中。

如何强制freemarker在处理过程中包含#included文件?

1 个答案:

答案 0 :(得分:1)

如果您只想包含文件,则应使用#include,而不是#import。例如:

<#include "includes/header.ftl">

请参阅Freemarker manual