Search in sources :

Example 6 with MultiTemplateLoader

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

the class GetOptionalTemplateTest method createConfiguration.

@Override
protected Configuration createConfiguration() throws Exception {
    Configuration cfg = super.createConfiguration();
    cfg.setTemplateLoader(new MultiTemplateLoader(new TemplateLoader[] { new StringTemplateLoader(), byteArrayTemplateLoader }));
    return cfg;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) StringTemplateLoader(freemarker.cache.StringTemplateLoader) Configuration(freemarker.template.Configuration) TemplateLoader(freemarker.cache.TemplateLoader) ByteArrayTemplateLoader(freemarker.cache.ByteArrayTemplateLoader) StringTemplateLoader(freemarker.cache.StringTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader)

Example 7 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project spring-framework by spring-projects.

the class FreeMarkerConfigurerTests method freeMarkerConfigurerWithResourceLoaderPath.

@Test
public void freeMarkerConfigurerWithResourceLoaderPath() throws Exception {
    freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
    freeMarkerConfigurer.afterPropertiesSet();
    Configuration cfg = freeMarkerConfigurer.getConfiguration();
    assertThat(cfg.getTemplateLoader()).isInstanceOf(MultiTemplateLoader.class);
    MultiTemplateLoader multiTemplateLoader = (MultiTemplateLoader) cfg.getTemplateLoader();
    assertThat(multiTemplateLoader.getTemplateLoader(0)).isInstanceOf(SpringTemplateLoader.class);
    assertThat(multiTemplateLoader.getTemplateLoader(1)).isInstanceOf(ClassTemplateLoader.class);
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) Test(org.junit.jupiter.api.Test)

Example 8 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project spring-framework by spring-projects.

the class FreeMarkerConfigurerTests method freeMarkerConfigurerWithResourceLoaderPath.

@Test
public void freeMarkerConfigurerWithResourceLoaderPath() throws Exception {
    freeMarkerConfigurer.setTemplateLoaderPath("file:/mydir");
    freeMarkerConfigurer.afterPropertiesSet();
    Configuration cfg = freeMarkerConfigurer.getConfiguration();
    assertThat(cfg.getTemplateLoader()).isInstanceOf(MultiTemplateLoader.class);
    MultiTemplateLoader multiTemplateLoader = (MultiTemplateLoader) cfg.getTemplateLoader();
    assertThat(multiTemplateLoader.getTemplateLoader(0)).isInstanceOf(SpringTemplateLoader.class);
    assertThat(multiTemplateLoader.getTemplateLoader(1)).isInstanceOf(ClassTemplateLoader.class);
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) Test(org.junit.jupiter.api.Test)

Example 9 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project alfresco-remote-api by Alfresco.

the class RepositoryTemplateProcessor method initConfig.

/**
 * Initialise FreeMarker Configuration
 */
protected void initConfig() {
    Configuration config = new Configuration();
    // setup template cache
    config.setCacheStorage(new StrongCacheStorage());
    config.setTemplateUpdateDelay(updateDelay);
    // setup template loaders
    List<TemplateLoader> loaders = new ArrayList<TemplateLoader>();
    for (Store apiStore : searchPath.getStores()) {
        TemplateLoader loader = apiStore.getTemplateLoader();
        if (loader == null) {
            throw new WebScriptException("Unable to retrieve template loader for Web Script store " + apiStore.getBasePath());
        }
        loaders.add(loader);
    }
    MultiTemplateLoader loader = new MultiTemplateLoader(loaders.toArray(new TemplateLoader[loaders.size()]));
    config.setTemplateLoader(loader);
    // use our custom object wrapper that can deal with QNameMap objects directly
    config.setObjectWrapper(new QNameAwareObjectWrapper());
    // rethrow any exception so we can deal with them
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    // turn off locale sensitive lookup - to save numerous wasted calls to nodeservice.exists()
    config.setLocalizedLookup(false);
    // set template encoding
    if (defaultEncoding != null) {
        config.setDefaultEncoding(defaultEncoding);
    }
    // set output encoding
    config.setOutputEncoding("UTF-8");
    config.setIncompatibleImprovements(new Version(2, 3, 20));
    config.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
    templateConfig = config;
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) Version(freemarker.template.Version) TemplateLoader(freemarker.cache.TemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) ArrayList(java.util.ArrayList) Store(org.springframework.extensions.webscripts.Store) StrongCacheStorage(freemarker.cache.StrongCacheStorage) QNameAwareObjectWrapper(org.alfresco.repo.template.QNameAwareObjectWrapper)

Example 10 with MultiTemplateLoader

use of freemarker.cache.MultiTemplateLoader in project jforum2 by rafaelsteil.

the class JForumBaseServlet method init.

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    try {
        String appPath = config.getServletContext().getRealPath("");
        debug = "true".equals(config.getInitParameter("development"));
        DOMConfigurator.configure(appPath + "/WEB-INF/log4j.xml");
        logger.info("Starting JForum. Debug mode is " + debug);
        ConfigLoader.startSystemglobals(appPath);
        ConfigLoader.startCacheEngine();
        // Configure the template engine
        Configuration templateCfg = new Configuration();
        templateCfg.setTemplateUpdateDelay(2);
        templateCfg.setSetting("number_format", "#");
        templateCfg.setSharedVariable("startupTime", new Long(new Date().getTime()));
        // Create the default template loader
        String defaultPath = SystemGlobals.getApplicationPath() + "/templates";
        FileTemplateLoader defaultLoader = new FileTemplateLoader(new File(defaultPath));
        String extraTemplatePath = SystemGlobals.getValue(ConfigKeys.FREEMARKER_EXTRA_TEMPLATE_PATH);
        if (StringUtils.isNotBlank(extraTemplatePath)) {
            // An extra template path is configured, we need a MultiTemplateLoader
            FileTemplateLoader extraLoader = new FileTemplateLoader(new File(extraTemplatePath));
            TemplateLoader[] loaders = new TemplateLoader[] { extraLoader, defaultLoader };
            MultiTemplateLoader multiLoader = new MultiTemplateLoader(loaders);
            templateCfg.setTemplateLoader(multiLoader);
        } else {
            // An extra template path is not configured, we only need the default loader
            templateCfg.setTemplateLoader(defaultLoader);
        }
        ModulesRepository.init(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR));
        this.loadConfigStuff();
        if (!this.debug) {
            templateCfg.setTemplateUpdateDelay(3600);
        }
        JForumExecutionContext.setTemplateConfig(templateCfg);
    } catch (Exception e) {
        throw new ForumStartupException("Error while starting JForum", e);
    }
}
Also used : MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) Configuration(freemarker.template.Configuration) TemplateLoader(freemarker.cache.TemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) ForumStartupException(net.jforum.exceptions.ForumStartupException) File(java.io.File) Date(java.util.Date) ServletException(javax.servlet.ServletException) ForumStartupException(net.jforum.exceptions.ForumStartupException)

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