Element from template file

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

Element from template file

jg
This post was updated on .
We have our own dialect and in most of the processors we start of by replacing the custom tag by a template file.
Current method to achieve this:

final Element result = new Element("div");
result.setAttribute("th:substituteby", "templatefile");

Is there a way to directly create an element from a template file, so without using "th:substituteby"?


Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Element form template file

Emanuel
Administrator
Do you mean to insert another template file into that 'result' div you created?  If so, then yes, you can use Thymeleaf's template resolution mechanism to get that template file, then insert it into your result div like so:

Template otherTemplate = arguments.getTemplateRepository().getTemplate(
    new TemplateProcessingParameters(arguments.getConfiguration(), templateName,
    arguments.getContext()));
result.addChild(otherTemplate.getDocument().getFirstElementChild());
Reply | Threaded
Open this post in threaded view
|

Re: Element form template file

danielfernandez
Administrator
Emanuel's solution is correct. You can look at the code of the "StandardSubstituteByAttrProcessor" class in order to see how this standard tag works for a more complete reference.

Just for documentation purposes, if what you wanted to include was the result of executing a different template engine (not Thymeleaf), you could insert into your template's DOM a node of the "Macro" class (org.thymeleaf.dom.Macro) with the textual content of executing your "other" template engine (as a String).

Macros are nodes that contain HTML and are not executed nor escaped at all by thymeleaf. They simply add a fragment of markup to a point in your DOM.

Regards,
Daniel.

jg
Reply | Threaded
Open this post in threaded view
|

Re: Element form template file

jg
In reply to this post by Emanuel
Thanks for the reponse! was exactly what I was looking for.

A small example for what I like to use it for:
Our dialect is used to define a sort of component library, e.g.:

<foo:switch />

which should be replaced by:

<div class="onoff-switch-container">
  <div class="onoff-switch">
    <input type="checkbox" checked="checked">
  </div>
</div>

defined in a html file, so I actually like to replace it, not add it to the result.

This is a very simple example, most components are a bit more complex. They can be combinations of other components, etc.
The idea is to have the components as simple as possible, reusable and easily adapted.
Reply | Threaded
Open this post in threaded view
|

Re: Element form template file

Emanuel
Administrator
To replace instead of just include, then you can add the following line after the example I gave above:

element.getParent().extractChild(element);

Given that 'element' is your custom <foo:switch/> element, then what that line will do is get the parent of foo:switch, pull foo:switch out of the DOM, and bring foo:switch's children (which is the content of the template we just added) up in place of it.