Resolve a template from within a processor ?

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

Resolve a template from within a processor ?

Jan Goyvaerts
Sorry for the enigmatic title, but I don't know how to phrase it otherwise.

Say I've for a simple custom element that will in the end generate a load of html markup.

<bar:complexWidget title="Far too complex to code" args="many other argurments">

Of course I can use code to create Node objects and nest them accordingly. But this tends to be a long an tedious process to get it all exactly right. Not to mention when the markup has to be modified afterwards. :-)

So, I'd like to make the generated markup a template as well. I'll extract the attributes from "complexWidget" and invoke the template engine/whatever/... to use the attributes on a template to obtain the corresponding markup. The processor will simply return the markup.

Is that possible in ThymeLeaf ?

TIA !

Jan


Reply | Threaded
Open this post in threaded view
|

Re: Resolve a template from within a processor ?

Emanuel
Administrator
I'm having a bit of trouble understanding what you're trying to do.  Are you trying to:

1. Pass attributes to another template.
2. Have those attributes alter that template in some way.
3. Return the altered template back to your custom element.

???

If I've got that right, then is it something you could do with the th:with and th:include processors?  That is, use the th:with to set some attributes, that will then be passed along to the template defined in th:with?  eg:

CurrentTemplate.html

<div th:with="arg1=${someExpression1}, arg2=${someExpression2}..." th:include="OtherTemplate.html">
  ...
</div>


OtherTemplate.html

<div th:if="${arg1}">
  <p>Show this first piece of HTML markup.</p>
  ...
</div>
<div th:if="${arg2}">
  <p>Show this second piece of HTML markup instead.</p>
   ...
</div>

So what you've got above in OtherTemplate.html, is some HTML that will change depending on what is passed to it, which is then inserted into CurrentTemplate.html  ???
Reply | Threaded
Open this post in threaded view
|

Re: Resolve a template from within a processor ?

Jan Goyvaerts
Part of the problem is probably I'm a bit new to Thymeleaf. Hence the confused question.

I didn't know about the attribute "th:with". So thanks for that !

HOWEVER... :-)

I'd like the original children Element being moved to a <ul>, to be wrapped each with a <li>.

<bar:parent>   <== use a template named "parent"
  <bar:child/> 
  <bar:child/>
  <bar:child/>   <== move those elements into a particular <ul> in the "parent" template
  <bar:child/>
</bar:parent>

Ideal would be to allow this kind of markup

<ul>
  <li th:each="child : children">
    ... how to include here the original <bar:child/>
  </li>
</ul>

Alas, I have no clue how to use Thymeleaf markup to cycle over a collection of Element objects.

----

A different thread in this forum gave me a coding solution:

1. Get the template from the template repository.
2. Get the parent element from the template Document.
3. Visit that parent element to find the <ul> element.
4. Reparent the <bar:child/> children to that element.
5. Set the children processable + the <ul> element.
6. Return the parent element.

Is this the way it is meant to be done ? Or can it be done completely in markup ?
Reply | Threaded
Open this post in threaded view
|

Re: Resolve a template from within a processor ?

Emanuel
Administrator
I think you should be able to do all of the steps entirely within the markup.  The th:include or th:substituteby processors can be used to target both a template AND a 'fragment' within the template so you don't need to do those 'get parent element, then search for the <ul> within it' steps.  For example:

Parent.html
<div th:include="Child.html :: childpart1">The fragment content of the other template will go here</div>

Child.html
<div th:fragment="childpart1">
  <p>Some markup you want included in your parent when asked for "childpart1".</p>
</div>
...
<div th:fragment="childpart2">
  <p>Some markup you want included when asked for "childpart2".</p>
</div>

That can cover steps 1-3: get another template, skip getting the parent element, go straight to selecting the fragment that you wanted to include.  (I'm still not too sure about what you're asking for with steps 4-6, but I hope that now you can see it's possible to pick out fragments from other templates within the markup, that you can find another solution for your problem.)

I think this topic is pretty well covered in the Using Thymeleaf docs (page of Thymeleaf docs: http://www.thymeleaf.org/documentation.html), section 8 on Template Layout and including/removing template fragments.