Search in sources :

Example 1 with TemplateLoader

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

the class FreeMarkerConfigurationFactory method createConfiguration.

/**
	 * Prepare the FreeMarker Configuration and return it.
	 * @return the FreeMarker Configuration object
	 * @throws IOException if the config file wasn't found
	 * @throws TemplateException on FreeMarker initialization failure
	 */
public Configuration createConfiguration() throws IOException, TemplateException {
    Configuration config = newConfiguration();
    Properties props = new Properties();
    // Load config file if specified.
    if (this.configLocation != null) {
        if (logger.isInfoEnabled()) {
            logger.info("Loading FreeMarker configuration from " + this.configLocation);
        }
        PropertiesLoaderUtils.fillProperties(props, this.configLocation);
    }
    // Merge local properties if specified.
    if (this.freemarkerSettings != null) {
        props.putAll(this.freemarkerSettings);
    }
    // setAllSharedVariables methods.
    if (!props.isEmpty()) {
        config.setSettings(props);
    }
    if (!CollectionUtils.isEmpty(this.freemarkerVariables)) {
        config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper()));
    }
    if (this.defaultEncoding != null) {
        config.setDefaultEncoding(this.defaultEncoding);
    }
    List<TemplateLoader> templateLoaders = new LinkedList<>(this.templateLoaders);
    // Register template loaders that are supposed to kick in early.
    if (this.preTemplateLoaders != null) {
        templateLoaders.addAll(this.preTemplateLoaders);
    }
    // Register default template loaders.
    if (this.templateLoaderPaths != null) {
        for (String path : this.templateLoaderPaths) {
            templateLoaders.add(getTemplateLoaderForPath(path));
        }
    }
    postProcessTemplateLoaders(templateLoaders);
    // Register template loaders that are supposed to kick in late.
    if (this.postTemplateLoaders != null) {
        templateLoaders.addAll(this.postTemplateLoaders);
    }
    TemplateLoader loader = getAggregateTemplateLoader(templateLoaders);
    if (loader != null) {
        config.setTemplateLoader(loader);
    }
    postProcessConfiguration(config);
    return config;
}
Also used : Configuration(freemarker.template.Configuration) TemplateLoader(freemarker.cache.TemplateLoader) FileTemplateLoader(freemarker.cache.FileTemplateLoader) MultiTemplateLoader(freemarker.cache.MultiTemplateLoader) SimpleHash(freemarker.template.SimpleHash) Properties(java.util.Properties) LinkedList(java.util.LinkedList)

Example 2 with TemplateLoader

use of freemarker.cache.TemplateLoader in project moco by dreamhead.

the class TemplateResourceReader method createTemplate.

private Template createTemplate(final MessageContent messageContent) throws IOException {
    TemplateLoader templateLoader = createTemplateLoader(messageContent);
    Configuration cfg = createConfiguration(templateLoader, messageContent.getCharset());
    return cfg.getTemplate(TEMPLATE_NAME);
}
Also used : Configuration(freemarker.template.Configuration) StringTemplateLoader(freemarker.cache.StringTemplateLoader) TemplateLoader(freemarker.cache.TemplateLoader)

Example 3 with TemplateLoader

use of freemarker.cache.TemplateLoader 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

TemplateLoader (freemarker.cache.TemplateLoader)3 Configuration (freemarker.template.Configuration)3 FileTemplateLoader (freemarker.cache.FileTemplateLoader)2 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)2 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 SimpleHash (freemarker.template.SimpleHash)1 File (java.io.File)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 ServletException (javax.servlet.ServletException)1 ForumStartupException (net.jforum.exceptions.ForumStartupException)1