Search in sources :

Example 6 with SimpleHash

use of freemarker.template.SimpleHash 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 7 with SimpleHash

use of freemarker.template.SimpleHash in project spring-framework by spring-projects.

the class FreeMarkerView method getTemplateModel.

/**
	 * Build a FreeMarker template model for the given model Map.
	 * <p>The default implementation builds a {@link SimpleHash}.
	 * @param model the model to use for rendering
	 * @param exchange current exchange
	 * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof
	 */
protected SimpleHash getTemplateModel(Map<String, Object> model, ServerWebExchange exchange) {
    SimpleHash fmModel = new SimpleHash(getObjectWrapper());
    fmModel.putAll(model);
    return fmModel;
}
Also used : SimpleHash(freemarker.template.SimpleHash)

Example 8 with SimpleHash

use of freemarker.template.SimpleHash in project spring-framework by spring-projects.

the class FreeMarkerView method doRender.

/**
	 * Render the FreeMarker view to the given response, using the given model
	 * map which contains the complete template model to use.
	 * <p>The default implementation renders the template specified by the "url"
	 * bean property, retrieved via {@code getTemplate}. It delegates to the
	 * {@code processTemplate} method to merge the template instance with
	 * the given template model.
	 * <p>Adds the standard Freemarker hash models to the model: request parameters,
	 * request, session and application (ServletContext), as well as the JSP tag
	 * library hash model.
	 * <p>Can be overridden to customize the behavior, for example to render
	 * multiple templates into a single view.
	 * @param model the model to use for rendering
	 * @param request current HTTP request
	 * @param response current servlet response
	 * @throws IOException if the template file could not be retrieved
	 * @throws Exception if rendering failed
	 * @see #setUrl
	 * @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
	 * @see #getTemplate(java.util.Locale)
	 * @see #processTemplate
	 * @see freemarker.ext.servlet.FreemarkerServlet
	 */
protected void doRender(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Expose model to JSP tags (as request attributes).
    exposeModelAsRequestAttributes(model, request);
    // Expose all standard FreeMarker hash models.
    SimpleHash fmModel = buildTemplateModel(model, request, response);
    if (logger.isDebugEnabled()) {
        logger.debug("Rendering FreeMarker template [" + getUrl() + "] in FreeMarkerView '" + getBeanName() + "'");
    }
    // Grab the locale-specific version of the template.
    Locale locale = RequestContextUtils.getLocale(request);
    processTemplate(getTemplate(locale), fmModel, response);
}
Also used : Locale(java.util.Locale) SimpleHash(freemarker.template.SimpleHash)

Example 9 with SimpleHash

use of freemarker.template.SimpleHash in project spring-framework by spring-projects.

the class FreeMarkerMacroTests method testExposeSpringMacroHelpers.

@Test
public void testExposeSpringMacroHelpers() throws Exception {
    FreeMarkerView fv = new FreeMarkerView() {

        @Override
        @SuppressWarnings("rawtypes")
        protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException {
            Map model = fmModel.toMap();
            assertTrue(model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext);
            RequestContext rc = (RequestContext) model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
            BindStatus status = rc.getBindStatus("tb.name");
            assertEquals("name", status.getExpression());
            assertEquals("juergen", status.getValue());
        }
    };
    fv.setUrl(TEMPLATE_FILE);
    fv.setApplicationContext(wac);
    fv.setExposeSpringMacroHelpers(true);
    Map<String, Object> model = new HashMap<>();
    model.put("tb", new TestBean("juergen", 99));
    fv.render(model, request, response);
}
Also used : HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) SimpleHash(freemarker.template.SimpleHash) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) DummyMacroRequestContext(org.springframework.web.servlet.view.DummyMacroRequestContext) RequestContext(org.springframework.web.servlet.support.RequestContext) BindStatus(org.springframework.web.servlet.support.BindStatus) HashMap(java.util.HashMap) Map(java.util.Map) Template(freemarker.template.Template) Test(org.junit.Test)

Example 10 with SimpleHash

use of freemarker.template.SimpleHash in project engine by craftercms.

the class RenderComponentDirective method getFullModel.

protected SimpleHash getFullModel(SiteItem component, Map<String, Object> templateModel, Map<String, Object> additionalModel) throws TemplateException {
    SimpleHash model = modelFactory.getObject();
    model.put(KEY_MODEL, component);
    model.put(KEY_CONTENT_MODEL, component);
    if (MapUtils.isNotEmpty(templateModel)) {
        model.putAll(templateModel);
    }
    if (MapUtils.isNotEmpty(additionalModel)) {
        model.putAll(additionalModel);
    }
    return model;
}
Also used : SimpleHash(freemarker.template.SimpleHash)

Aggregations

SimpleHash (freemarker.template.SimpleHash)18 Template (freemarker.template.Template)5 IOException (java.io.IOException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Writer (java.io.Writer)3 Locale (java.util.Locale)3 BufferedWriter (java.io.BufferedWriter)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ServletException (javax.servlet.ServletException)2 JForumContext (net.jforum.context.JForumContext)2 RequestContext (net.jforum.context.RequestContext)2 ResponseContext (net.jforum.context.ResponseContext)2 WebRequestContext (net.jforum.context.web.WebRequestContext)2 WebResponseContext (net.jforum.context.web.WebResponseContext)2 ExceptionWriter (net.jforum.exceptions.ExceptionWriter)2