Mechanism for configuring template resolution on a per-template basis
See original GitHub issueA mechanism called template resolution attributes has been added to Thymeleaf 3.0 that will allow further configuration of the way a template is resolved, specifying this for each call of the template engine’s process(...)
methods.
The idea is that at the process(...)
call a series of configurations (attributes) can be specified by means of a map in the TemplateSpec
class:
public TemplateSpec(
final String template, final Set<String> templateSelectors,
final TemplateMode templateMode,
final Map<String, Object> templateResolutionAttributes) {
...
}
This template specification can then be used for calling the template engine:
templateEngine.process(templateSpec, context, writer);
And the result will be not only that the specified Map
will be available at any time for template processing artifacts from the ITemplateContext
:
public interface ITemplateContext extends IExpressionContext {
...
public Map<String, Object> getTemplateResolutionAttributes();
...
}
…but also that every call to the ITemplateResolver
will include this map:
public interface ITemplateResolver {
...
public TemplateResolution resolveTemplate(
final IEngineConfiguration configuration,
final String ownerTemplate, final String template,
final Map<String, Object> templateResolutionAttributes);
...
Importantly, note that not only this map will be included in the ITemplateResolver#resolveTemplate(...)
call for the first-level template, but also for every other template from which a fragment might be included during the processing of the first-level template.
Also note that these template resolution attributes will be inclued as a part of the template’s cache key, so that the same template resolved with different resolution attributes will result in different cache entries. It is important therefore that the values of these attributes correctly implement the equals(...)
and hashCode()
methods.
This should solve scenarios like the ones detailed at #329 and #380.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
@danielfernandez I’m not using them directly since I use the Spring integration with
ThymeleafView
, so it’s not my choice. What I need would be some hook where I can add template resolution attributes. I’m now building a proxyITemplateEngine
that does that, but that doesn’t feel like the optimal solution. Thanks for all your work on Thymeleaf, it’s a great thing!Resurrecting this issue in case someone else needs help adding
templateResolutionAttributes
.The solution that we came up with is to create a new template engine that delegates to the out of box Thymeleaf
TemplateEngine
. In doing so, we can modify thetemplateResolutionAttributes
and then pass them along.To configure the new
DelegatingTemplateEngine
we added the following configuration: