Search in sources :

Example 11 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project wombat by PLOS.

the class SpringConfiguration method freeMarkerConfig.

@Bean
public FreeMarkerConfig freeMarkerConfig(ServletContext servletContext, SiteSet siteSet, IsDevFeatureEnabledDirective isDevFeatureEnabledDirective, SiteLinkDirective siteLinkDirective, RenderCssLinksDirective renderCssLinksDirective, RenderJsDirective renderJsDirective, BuildInfoDirective buildInfoDirective, FetchHtmlDirective fetchHtmlDirective, ThemeConfigDirective themeConfigDirective, AppLinkDirective appLinkDirective, ArticleExcerptTransformDirective articleExcerptTransformDirective, GlobalConfigDirective globalConfigDirective) throws IOException {
    SiteTemplateLoader loader = new SiteTemplateLoader(servletContext, siteSet);
    FreeMarkerConfigurer config = new FreeMarkerConfigurer();
    config.setPreTemplateLoaders(loader);
    // Freemarker custom directives used throughout the app.
    // TODO: should all of these be their own @Beans?  I'm only doing that now for
    // ones that have dependencies on spring-injection.
    ImmutableMap.Builder<String, Object> variables = ImmutableMap.builder();
    variables.put("formatJsonDate", new Iso8601DateDirective());
    variables.put("replaceParams", new ReplaceParametersDirective());
    variables.put("siteLink", siteLinkDirective);
    variables.put("isDevFeatureEnabled", isDevFeatureEnabledDirective);
    variables.put("cssLink", new CssLinkDirective());
    variables.put("renderCssLinks", renderCssLinksDirective);
    variables.put("js", new JsDirective());
    variables.put("renderJs", renderJsDirective);
    variables.put("buildInfo", buildInfoDirective);
    variables.put("fetchHtml", fetchHtmlDirective);
    variables.put("themeConfig", themeConfigDirective);
    variables.put("appLink", appLinkDirective);
    variables.put("abbreviatedName", new AbbreviatedNameDirective());
    variables.put("xform", articleExcerptTransformDirective);
    variables.put("globalConfig", globalConfigDirective);
    config.setFreemarkerVariables(variables.build());
    return config;
}
Also used : AbbreviatedNameDirective(org.ambraproject.wombat.freemarker.AbbreviatedNameDirective) SiteTemplateLoader(org.ambraproject.wombat.config.site.SiteTemplateLoader) CssLinkDirective(org.ambraproject.wombat.freemarker.asset.CssLinkDirective) ReplaceParametersDirective(org.ambraproject.wombat.freemarker.ReplaceParametersDirective) Iso8601DateDirective(org.ambraproject.wombat.freemarker.Iso8601DateDirective) JsDirective(org.ambraproject.wombat.freemarker.asset.JsDirective) RenderJsDirective(org.ambraproject.wombat.freemarker.asset.RenderJsDirective) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) ImmutableMap(com.google.common.collect.ImmutableMap) Bean(org.springframework.context.annotation.Bean)

Example 12 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project Ganster-CMS by Gangster-trio.

the class FreemarkerConfig method loadPathConfig.

@Bean
public FreeMarkerConfigurer loadPathConfig() {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    String path = settingService.get(CmsConst.SKIN_PATH_SETTING);
    String DEFAULT_SKIN_PATH = "classpath:templates";
    if (!path.isEmpty()) {
        configurer.setTemplateLoaderPaths(DEFAULT_SKIN_PATH, path);
        LOGGER.info("Gangster CMS : templates path = {},{}", DEFAULT_SKIN_PATH, path);
    } else {
        configurer.setTemplateLoaderPath(DEFAULT_SKIN_PATH);
    // configurer.setTemplateLoaderPath("file:/home/bigmeng/Desktop/templates/");
    }
    configurer.setDefaultEncoding("UTF-8");
    return configurer;
}
Also used : FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) Bean(org.springframework.context.annotation.Bean)

Example 13 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project spring-boot by spring-projects.

the class FreeMarkerAutoConfigurationServletIntegrationTests method renderTemplate.

@Test
void renderTemplate() throws Exception {
    load();
    FreeMarkerConfigurer freemarker = this.context.getBean(FreeMarkerConfigurer.class);
    StringWriter writer = new StringWriter();
    freemarker.getConfiguration().getTemplate("message.ftlh").process(new DataModel(), writer);
    assertThat(writer.toString()).contains("Hello World");
}
Also used : StringWriter(java.io.StringWriter) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) Test(org.junit.jupiter.api.Test)

Example 14 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project spring-boot by spring-projects.

the class FreeMarkerServletWebConfiguration method freeMarkerConfigurer.

@Bean
@ConditionalOnMissingBean(FreeMarkerConfig.class)
FreeMarkerConfigurer freeMarkerConfigurer() {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    applyProperties(configurer);
    return configurer;
}
Also used : FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) ConditionalOnMissingFilterBean(org.springframework.boot.autoconfigure.web.servlet.ConditionalOnMissingFilterBean) Bean(org.springframework.context.annotation.Bean)

Example 15 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project leopard by tanhaichao.

the class FreeMarkerUtil method getFreeMarkerConfigurer.

public static FreeMarkerConfigurer getFreeMarkerConfigurer(ApplicationContext applicationContext, String templateLoaderPath) {
    Map<String, Object> freemarkerVariables = new HashMap<String, Object>();
    freemarkerVariables.put("xml_escape", "fmXmlEscape");
    freemarkerVariables.put("replaceParam", new ReplaceParamTemplateMethod());
    freemarkerVariables.put("timeAgo", new TimeAgoTemplateMethod());
    freemarkerVariables.putAll(listTemplateMethod(applicationContext));
    Properties freemarkerSettings = new Properties();
    freemarkerSettings.put("template_update_delay", "1");
    freemarkerSettings.put("defaultEncoding", "UTF-8");
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath(templateLoaderPath);
    configurer.setFreemarkerVariables(freemarkerVariables);
    configurer.setFreemarkerSettings(freemarkerSettings);
    try {
        configurer.afterPropertiesSet();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (TemplateException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return configurer;
}
Also used : HashMap(java.util.HashMap) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) TimeAgoTemplateMethod(io.leopard.web.freemarker.template.TimeAgoTemplateMethod) Properties(java.util.Properties) ReplaceParamTemplateMethod(io.leopard.web.freemarker.template.ReplaceParamTemplateMethod) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer)

Aggregations

FreeMarkerConfigurer (org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer)18 Bean (org.springframework.context.annotation.Bean)13 Properties (java.util.Properties)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Iso8601DateDirective (org.ambraproject.wombat.freemarker.Iso8601DateDirective)3 ReplaceParametersDirective (org.ambraproject.wombat.freemarker.ReplaceParametersDirective)3 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 SiteTemplateLoader (org.ambraproject.wombat.config.site.SiteTemplateLoader)2 AbbreviatedNameDirective (org.ambraproject.wombat.freemarker.AbbreviatedNameDirective)2 CssLinkDirective (org.ambraproject.wombat.freemarker.asset.CssLinkDirective)2 JsDirective (org.ambraproject.wombat.freemarker.asset.JsDirective)2 RenderJsDirective (org.ambraproject.wombat.freemarker.asset.RenderJsDirective)2 Test (org.junit.jupiter.api.Test)2 FileTemplateLoader (freemarker.cache.FileTemplateLoader)1 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1 BeansWrapper (freemarker.ext.beans.BeansWrapper)1 Configuration (freemarker.template.Configuration)1 TemplateException (freemarker.template.TemplateException)1