Search in sources :

Example 11 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project pratilipi by Pratilipi.

the class FreeMarkerUtil method getConfiguration.

private static Configuration getConfiguration() throws UnexpectedServerException {
    if (cfg == null) {
        FileTemplateLoader ftl;
        try {
            ftl = new FileTemplateLoader(new File("."));
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Failed to set template directory.", e);
            throw new UnexpectedServerException();
        }
        ClassTemplateLoader ctl = new ClassTemplateLoader(FreeMarkerUtil.class.getClassLoader(), "");
        MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] { ftl, ctl });
        cfg = new Configuration(Configuration.VERSION_2_3_22);
        cfg.setTemplateLoader(mtl);
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }
    return cfg;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) Configuration(freemarker.template.Configuration) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader) File(java.io.File)

Example 12 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project drill by axbaretto.

the class DrillRestServer method getFreemarkerConfiguration.

/**
 * Creates freemarker configuration settings,
 * default output format to trigger auto-escaping policy
 * and template loaders.
 *
 * @param servletContext servlet context
 * @return freemarker configuration settings
 */
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
    configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
    List<TemplateLoader> loaders = new ArrayList<>();
    loaders.add(new WebappTemplateLoader(servletContext));
    loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
    try {
        loaders.add(new FileTemplateLoader(new File("/")));
    } catch (IOException e) {
        logger.error("Could not set up file template loader.", e);
    }
    configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
    return configuration;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) Configuration(freemarker.template.Configuration) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader) File(java.io.File)

Example 13 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project wombat by PLOS.

the class SiteTemplateLoader method buildLoaders.

private static ImmutableMap<String, TemplateLoader> buildLoaders(ServletContext servletContext, SiteSet siteSet) throws IOException {
    ImmutableMap.Builder<String, TemplateLoader> builder = ImmutableMap.builder();
    // Add the loader for the application root page
    builder.put("", new WebappTemplateLoader(servletContext, "/WEB-INF/themes/root/app/"));
    // Add loader for each site
    for (Site site : siteSet.getSites()) {
        Theme leaf = site.getTheme();
        List<TemplateLoader> loaders = Lists.newArrayList();
        for (Theme theme : leaf.getInheritanceChain()) {
            loaders.add(theme.getTemplateLoader());
        }
        MultiTemplateLoader multiLoader = new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]));
        builder.put(site.getKey(), multiLoader);
    }
    return builder.build();
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) DelegatingTemplateLoader(org.ambraproject.wombat.config.DelegatingTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Theme(org.ambraproject.wombat.config.theme.Theme) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 14 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project revapi by revapi.

the class TextReporter method createFreeMarkerConfiguration.

/**
 * Creates a new FreeMarker configuration.
 * By default, it is configured as follows:
 * <ul>
 * <li>compatibility level is set to 2.3.23
 * <li>the object wrapper is configured to expose fields
 * <li>API builtins are enabled
 * <li>there are 2 template loaders - 1 for loading templates from /META-INF using a classloader and a second
 *     one to load templates from files.
 * </ul>
 * @return
 */
protected Configuration createFreeMarkerConfiguration() {
    DefaultObjectWrapperBuilder bld = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_23);
    bld.setExposeFields(true);
    Configuration freeMarker = new Configuration(Configuration.VERSION_2_3_23);
    freeMarker.setObjectWrapper(bld.build());
    freeMarker.setAPIBuiltinEnabled(true);
    freeMarker.setTemplateLoader(new MultiTemplateLoader(new TemplateLoader[] { new ClassTemplateLoader(getClass(), "/META-INF"), new NaiveFileTemplateLoader() }));
    return freeMarker;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) DefaultObjectWrapperBuilder(freemarker.template.DefaultObjectWrapperBuilder)

Example 15 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project drill by apache.

the class DrillRestServer method getFreemarkerConfiguration.

/**
 * Creates freemarker configuration settings,
 * default output format to trigger auto-escaping policy
 * and template loaders.
 *
 * @param servletContext servlet context
 * @return freemarker configuration settings
 */
private Configuration getFreemarkerConfiguration(ServletContext servletContext) {
    Configuration configuration = new Configuration(Configuration.VERSION_2_3_26);
    configuration.setOutputFormat(HTMLOutputFormat.INSTANCE);
    List<TemplateLoader> loaders = new ArrayList<>();
    loaders.add(new WebappTemplateLoader(servletContext));
    loaders.add(new ClassTemplateLoader(DrillRestServer.class, "/"));
    try {
        loaders.add(new FileTemplateLoader(new File("/")));
    } catch (IOException e) {
        logger.error("Could not set up file template loader.", e);
    }
    configuration.setTemplateLoader(new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()])));
    return configuration;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) Configuration(freemarker.template.Configuration) WebappTemplateLoader(freemarker.cache.WebappTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader) File(java.io.File)

Aggregations

MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)15 TemplateLoader (freemarker.cache.TemplateLoader)12 Configuration (freemarker.template.Configuration)11 FileTemplateLoader (freemarker.cache.FileTemplateLoader)8 File (java.io.File)7 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)6 WebappTemplateLoader (freemarker.cache.WebappTemplateLoader)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 StringTemplateLoader (freemarker.cache.StringTemplateLoader)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 Test (org.junit.jupiter.api.Test)2 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 ByteArrayTemplateLoader (freemarker.cache.ByteArrayTemplateLoader)1 StrongCacheStorage (freemarker.cache.StrongCacheStorage)1 URLTemplateLoader (freemarker.cache.URLTemplateLoader)1 BeanModel (freemarker.ext.beans.BeanModel)1 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)1 DefaultObjectWrapperBuilder (freemarker.template.DefaultObjectWrapperBuilder)1 TemplateException (freemarker.template.TemplateException)1