Porting to thymeleaf3 gives me some headache

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Porting to thymeleaf3 gives me some headache

eiswind
I once wrote a custom renderer adaption to use thymeleaf 2 in magnolia cms.
https://github.com/eiswind/magnolia-thymeleaf-renderer
I have a piece of code where I render a single fragment:

final IWebContext context = new SpringWebContext(request, response, servletContext, MgnlContext.getWebContext()
                .getRequest().getLocale(), vars, getApplicationContext());
final StandardFragment fragment = StandardFragmentProcessor.computeStandardFragmentSpec(
                    engine.getConfiguration(), new ProcessingContext(context), templateScript, null, "th:"
                            + StandardFragmentAttrProcessor.ATTR_NAME);

// and pass the fragment name and spec then onto the engine
engine.process(fragment.getTemplateName(), context, fragment.getFragmentSpec(), out);


But looking at the new IncludeProcessors I need a templateContext to resolve the fragment (in computeFragment for the exprssion parsing) and that at this point I don't have one.

How would I allow processing of a single Fragment in a Spring Context with thymeleaf3? Any hint where to look at would be very appreciated.

So long, Thomas
Reply | Threaded
Open this post in threaded view
|

Re: Porting to thymeleaf3 gives me some headache

danielfernandez
Administrator
There are two possibilities:

1. If you want your "templateScript" literal (which I suppose is the "template :: fragment" specification you want to use) to be able to use exactly all the same syntax possibilities as in a th:include/th:insert/th:replace, including variable expressions like "${templateThing} :: ${fragmentThing}", then you can follow the example of the ThymeleafView class, which does exactly that: https://github.com/thymeleaf/thymeleaf-spring/blob/ea0e1effc294ccc451e2625cd43c74b7ef5cb6c9/thymeleaf-spring4/src/main/java/org/thymeleaf/spring4/view/ThymeleafView.java#L253-L299

2. If otherwise you have a simpler scenario and you simply have a template name on one hand and a fragment specification on the other, you can simply use the TemplateEngine#process() method that allows the specification of a "Set<String> templateSelectors" argument (a fragment specification is a template selector).

Regards,
Daniel.