【发布时间】:2014-09-15 15:00:45
【问题描述】:
有没有什么方法可以直接加载 Freemarker 模板,而无需首先加载要从中加载模板的 Directory 或设置相对于要加载的模板的 Class。
即有什么方法可以让我加载模板,如
Template template = getTemplate("PathToTemplate/myTemplate.ftl");
我需要这个,因为用户指定了ftl 文件的完整路径。所以,首先我必须将目录名和文件名分开,然后我正在做
Configuration cfg = new Configuration();
int indexOfLast = templatePath.lastIndexOf("\\");
String dir = templatePath;
String fileName="";
if(indexOfLast>=0){
dir = templatePath.substring(0,indexOfLast);
fileName= templatePath.substring(indexOfLast+1,templatePath.length());
}
cfg.setDirectoryForTemplateLoading(new File(dir));
Template template = cfg.getTemplate(fileName);
我不想做这一切。
【问题讨论】:
-
loading the Freemarker templates directly是什么意思 - 你的PathToTemplate应该在哪里?它与某物有关吗?还是绝对的? -
@KonstantinV.Salikhov 它在连接的网络上。
Loading templates directly表示我不想要两个步骤。首先设置加载模板的目录或路径,然后config.getTemplate('templateFile.ftl')获取模板。而是something.getTemplate('dir path/templateFile.ftl')
标签: java freemarker