Custom context on included fragments

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

Custom context on included fragments

eiswind
This post was updated on .
Hi all,

I'm just getting started with thymeleaf. What I am working on is a prototype for integrating thymeleaf/spring with magnolia cms. Therefore I created a custom include processor, that has access to the magnolia cms contents and tried to include the fragment with a new context, that itselfs includes the magnolia cms content to be rendered in the inclusion. Unfortunately I couldn't access the variables from the new context in the fragment, I guess I made something wrong there. I worked around by cloning the fragment nodes, processing them as a new Document, and then cloning them back in the original document which seems a little expensive, but at least works for now.

What I wonder now is the scope of the fragment processing, and when does fragment processing happen. Is there a simpler way to include a fragment with "scoped variables"?

If you look at
https://github.com/eiswind/magnolia-blossom-thymeleaf-proto/blob/master/app/module/src/main/java/thymeleaf/processor/AbstractRecursiveInclusionProcessor.java
maybe you can give me a hint how this can be done with less cloning effort?


Looking forward for some hints :)
Reply | Threaded
Open this post in threaded view
|

Re: Custom context on included fragments

Emanuel
Administrator
From what I've managed to find out, processing is done top-down and depth-first, based on the DOM structure of your pages.  Any variables declared in your markup or by processors, are available to other processors of lower priority in the element they're declared, and all child elements.

How were you setting the variables in your processors before you did your cloning workaround?  I've used ProcessorResult.setLocalVariables(Map<String,Object>) in my dialect, which I think I copied from how the th:with processor works.
Reply | Threaded
Open this post in threaded view
|

Re: Custom context on included fragments

eiswind
Thanks for clearing up my understanding a little! That helped a lot. I got it working now.

But when I try to do it like th:substitute (th:include style is fine) the variables go lost, I guess they get set on the original element that was replaced by my processor.

Do you have an idea how I could workaround? I guess this won't be easy to fix.
Reply | Threaded
Open this post in threaded view
|

Re: Custom context on included fragments

eiswind
I could workaround by setting local variables directly on the included nodes.

List<Node> newNodes = new ArrayList<Node>();
        newNodes.add(commentNode);
        newNodes.addAll(fragmentNodes);

        commentNode = new Comment(closeTag);
        newNodes.add(commentNode);
       // element.setRecomputeProcessorsImmediately(true);
       

        element.setChildren(newNodes);
        element.getParent().extractChild(element);


       
       
        for(Node node:fragmentNodes){
            node.setAllNodeLocalVariables(vars);
        }