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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations