public FreemarkerDefaultConfigurationFactory(ServletContext servletContext) { super(); // Create different loaders. final List<TemplateLoader> loaders = new ArrayList<>(); if (servletContext != null) { loaders.add(new WebappTemplateLoader(servletContext)); } loaders.add(new ClassTemplateLoader(FreemarkerDefaultConfigurationFactory.class, "/")); try { loaders.add(new FileTemplateLoader(new File("/"))); } catch (IOException e) { // NOOP } // Create Base configuration. configuration = new Configuration(); configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]))); }
"Template loader path [" + path + "] resolved to file path [" + file.getAbsolutePath() + "]"); return new FileTemplateLoader(file);
/** * Sets the file system directory from which to load templates. This is equivalent to * {@code setTemplateLoader(new FileTemplateLoader(dir))}, so see * {@link FileTemplateLoader#FileTemplateLoader(File)} for more details. * * <p> * Note that FreeMarker can load templates from non-file-system sources too. See * {@link #setTemplateLoader(TemplateLoader)} from more details. * * <p> * Note that this shouldn't be used for loading templates that are coming from a WAR; use * {@link #setServletContextForTemplateLoading(Object, String)} then. Servlet containers might not unpack the WAR * file, in which case you clearly can't access the contained files via {@link File}. Even if the WAR is unpacked, * the servlet container might not expose the location as a {@link File}. * {@link #setServletContextForTemplateLoading(Object, String)} on the other hand will work in all these cases. */ public void setDirectoryForTemplateLoading(File dir) throws IOException { TemplateLoader tl = getTemplateLoader(); if (tl instanceof FileTemplateLoader) { String path = ((FileTemplateLoader) tl).baseDir.getCanonicalPath(); if (path.equals(dir.getCanonicalPath())) return; } setTemplateLoader(new FileTemplateLoader(dir)); }
public GeoServerTemplateLoader(Class caller, GeoServerDataDirectory dd) throws IOException { this.dd = dd; // create a file template loader to delegate to fileTemplateLoader = new FileTemplateLoader(dd.root()); // grab the catalog and store a reference catalog = (Catalog) GeoServerExtensions.bean("catalog"); // create a class template loader to delegate to if (caller != null) { classTemplateLoader = new ClassTemplateLoader(caller, ""); } }
"Template loader path [" + path + "] resolved to file path [" + file.getAbsolutePath() + "]"); return new FileTemplateLoader(file);
} else if (pureTemplatePath.startsWith(TEMPLATE_PATH_PREFIX_FILE)) { String filePath = pureTemplatePath.substring(TEMPLATE_PATH_PREFIX_FILE.length()); templateLoader = new FileTemplateLoader(new File(filePath)); } else if (pureTemplatePath.startsWith("[") && cfg.getIncompatibleImprovements().intValue() >= _TemplateAPI.VERSION_INT_2_3_22) {
case FILE: try { cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(config.getPath()))); } catch (IOException e) { throw new IORuntimeException(e);
case FILE: try { cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(config.getPath()))); } catch (IOException e) { throw new IORuntimeException(e);
FileTemplateLoader fileTemplateLoader = new FileTemplateLoader(new File(srcDir));
final TemplateLoader ftl = new FileTemplateLoader(new File(templatePath)); final TemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] { ctl,
public void addTemplateDirectory(File dir) throws IOException { templateLoaders_.push(new FileTemplateLoader(dir)); templatedirs_.push(dir); }
import freemarker.cache.*; // template loaders live in this package ... FileTemplateLoader ftl1 = new FileTemplateLoader(new File("/tmp/templates")); FileTemplateLoader ftl2 = new FileTemplateLoader(new File("/usr/data/templates")); ClassTemplateLoader ctl = new ClassTemplateLoader(getClass(), ""); TemplateLoader[] loaders = new TemplateLoader[] { ftl1, ftl2, ctl }; MultiTemplateLoader mtl = new MultiTemplateLoader(loaders); cfg.setTemplateLoader(mtl);
@Bean public freemarker.template.Configuration getConfiguration() { final freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22); try { configuration.setTemplateLoader(new FileTemplateLoader(new File(templatesPath))); } catch (final IOException e) { LOGGER.warn("Error occurred while trying to read email templates directory", e); } return configuration; }
@Bean public freemarker.template.Configuration getConfiguration() { final freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22); try { configuration.setTemplateLoader(new FileTemplateLoader(new File(templatesPath))); } catch (final IOException e) { LOGGER.warn("Error occurred while trying to read email templates directory", e); } return configuration; }
@Bean public freemarker.template.Configuration getConfiguration() { final freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22); try { configuration.setTemplateLoader(new FileTemplateLoader(new File(templatesPath))); } catch (final IOException e) { LOGGER.warn("Error occurred while trying to read email templates directory", e); } return configuration; }
@Bean public freemarker.template.Configuration getConfiguration() { final freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22); try { configuration.setTemplateLoader(new FileTemplateLoader(new File(templatesPath))); } catch (final IOException e) { LOGGER.warn("Error occurred while trying to read email templates directory", e); } return configuration; } }
@Override protected TemplateLoader newDelegate() { if (basePath == null) { throw new IllegalArgumentException("Can't initialize FileTemplateLoader: basePath is not set. Please set the basePath property."); } try { return new FileTemplateLoader(new File(basePath), allowLinking); } catch (IOException e) { log.error("Can't initialize FileTemplateLoader: {}", e.getMessage(), e); throw new IllegalArgumentException("Can't initialize FileTemplateLoader: " + e.getMessage()); } } }
/** * Set the explicit directory from which to load templates. */ public void setDirectoryForTemplateLoading(File dir) throws IOException { TemplateLoader tl = getTemplateLoader(); if (tl instanceof FileTemplateLoader) { String path = ((FileTemplateLoader) tl).baseDir.getCanonicalPath(); if (path.equals(dir.getCanonicalPath())) return; } setTemplateLoader(new FileTemplateLoader(dir)); }
private static TemplateLoader createDefaultTemplateLoader() { try { return new FileTemplateLoader(); } catch(Exception e) { logger.warn("Could not create a file template loader for current directory", e); return null; } }
Configuration configuration = new Configuration(); FileTemplateLoader templateLoader = new FileTemplateLoader(new File(YOUR_BASE_TEMPLATE_DIR)); configuration.setTemplateLoader(templateLoader); freemarker.template.Template template = configuration.getTemplate(YOUR_TEMPLATE_NAME); template.process(datamodel, writer);