Reputation: 48864
I've been using FreeMarker for a little while now, but there's one glaring piece of functionality that is either missing or I just can't figure out (I hope the latter!). If you pass cfg.getTemplate() an absolute path, it just doesn't work. I know you can specify a template directory, but I can't afford to do that, my use case could deal with files in any directory. Is there any way to set FreeMarker to render absolute paths the way any user would expect?
Upvotes: 1
Views: 6575
Reputation: 577
The problem with the accepted solution is, that the path name is destroyed in FreeMarker before the TemplateLoader is used. See TemplateCache:
name = normalizeName(name);
if(name == null) {
return null;
}
Template result = null;
if (templateLoader != null) {
result = getTemplate(templateLoader, name, locale, encoding, parseAsFTL);
}
So I think it is better to use a solution as proposed in this answer
For example
Configuration config = new Configuration();
File templateFile = new File(templateFilename);
File templateDir = templateFile.getParentFile();
if ( null == templateDir ){
templateDir = new File("./");
}
config.setDirectoryForTemplateLoading(templateDir);
Template template = config.getTemplate(templateFile.getName());
Upvotes: 0
Reputation: 777
Actually it remove the beginning "/" so you need to add it back in
public Object findTemplateSource(String name) throws IOException {
File source = new File("/" + name);
return source.isFile() ? source : null;
}
Upvotes: 2
Reputation: 6871
I had to use the absolute path because the templating is happening in an Ant script and the templates are on the file system and discovered with an Ant fileset. I guess these are quite some unique requirements...
Anyway,for the posterity (as long as SO is up), here's a solution that works:
public class TemplateAbsolutePathLoader implements TemplateLoader {
public Object findTemplateSource(String name) throws IOException {
File source = new File(name);
return source.isFile() ? source : null;
}
public long getLastModified(Object templateSource) {
return ((File) templateSource).lastModified();
}
public Reader getReader(Object templateSource, String encoding)
throws IOException {
if (!(templateSource instanceof File)) {
throw new IllegalArgumentException("templateSource is a: " + templateSource.getClass().getName());
}
return new InputStreamReader(new FileInputStream((File) templateSource), encoding);
}
public void closeTemplateSource(Object templateSource) throws IOException {
// Do nothing.
}
}
and the initialization is:
public String generate(File template) {
Configuration cfg = new Configuration();
cfg.setTemplateLoader(new TemplateAbsolutePathLoader());
Template tpl = cfg.getTemplate(template.getAbsolutePath());
// ...
}
Upvotes: 6
Reputation: 100736
Freemarker uses FileTemplateLoader by default that will not allow you to get templates from outside of "base" directory (which by default is taken from 'user.dir' system property, so it's your home dir). What you can do is:
Upvotes: 1