Code reuse in templates

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

Code reuse in templates

borillo
Hi all,

I have reviewed the documentation and i couldn't find anything like the functionality that i'm going to describe. Sorry if this is already implemented :)

I want to have a fragment of the HTML page in a separate file. This fragment will use a varible called ${text}.

Then, I want to include this fragment in a page, but i don't want the fragment to query for ${text} variable in the global variable scope. Instead, i want to provide an input value for this tamplate.

Something similar to:

   <div th:include="templatename" th:paramName="text" th:paramValue="23" />

It's the same situation that in XSL, where you can match a template of call it with params:

   http://www.w3schools.com/xsl/el_call-template.asp

Any tips on this?

Best regards,
  Ricardo
Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

Zemi
Administrator
Try this:

   <div th:with="text=23" th:include="templatename" />

I think that should work, but if it doesn't try:

   <div th:with="text=23">
      <div th:include="templatename" />
   </div>

Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

borillo
Hi Zemi,

It works like a charm :)

I don't know why, but the first reduced form is not working for me ...
I didn't want to introduce new markup so i use this construction:

      <div th:with="tagname=${urlBase}" th:remove="tag">
        <div th:substituteby="mytemplate" />
      </div>

As i said, it works without problems, but i really prefer to use this first form that you suggest:

      <div th:with="tagname=${urlBase}" th:substituteby="mytemplate" />

Do you think this is possible?

Best regards,
  Ricardo

Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

Zemi
Administrator
I think it will work using th:include instead of th:substitute

      <div th:with="tagname=${urlBase}" th:include="mytemplate" />

because the first one also replaces the th:with attribute.

Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

Emanuel
Administrator
In reply to this post by borillo
It might not work if you use th:with with th:substituteby because th:substituteby replaces the entire tag with the tag you referenced, and runs before th:with (chapter 10 of the Using Thymeleaf doc on attribute precedence).
Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

borillo
Ok. Thanks for the information :)