Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf, fragments and default parameters

I have created fragments.html file. It contains the following fragment:

<div th:fragment="my_fragment(content)">     <p th:text="${content}"></p> </div> 

I put the above fragment into my view file:

<div th:replace="fragments :: my_fragment('test')"></div> 

Now, I want to pass two parameters to my_fragment, but I must ensure backward compatibility.

I tried to solve the problem as follows:

<div th:fragment="my_fragment(content, defaultParameter='default value')">     <p th:text="${content}"></p> </div> 

Unfortunelly, the above solution generated error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Cannot resolve fragment. Signature "my_fragment (content,defaultParameter='default value')" declares 2 parameters, but fragment selection specifies 1 parameters. Fragment selection does not correctly match.

Any idea?

like image 980
user3364391 Avatar asked Feb 28 '14 10:02

user3364391


2 Answers

Thymeleaf allows a signature of a fragment without explicit parameters like this:

<div th:fragment="my_fragment">     <p th:text="${content}"></p>     <p th:text="${defaultParameter}"></p> </div> 

To call this fragment and pass content and defaultParameter you may call the fragment as follows:

<!-- both parameters not specified --> <div th:replace="fragments :: my_fragment"></div> <!-- only content parameter specified --> <div th:replace="fragments :: my_fragment(content='a')"></div> <!-- both parameters specified --> <div th:replace="fragments :: my_fragment(content='a', defaultParameter='b')"></div> 

But the below will not work:

<div th:replace="fragments :: my_fragment('a', 'b')"></div> 

And the message is self-explanatory:

 Signature "my_fragment" declares no parameters, but fragment selection did specify parameters in a synthetic manner (without names), which is not correct due to the fact parameters cannot be assigned names unless signature specifies these names.  

So in case you want to maintain backward compatibility, you should use named parameters while calling a fragment and do not specify parameters in a signature of a fragment.

like image 127
Rafal Borowiec Avatar answered Oct 03 '22 02:10

Rafal Borowiec


The best way to allow optional parameter for a fragment is to declare them with "th:with" and describe them with meaningful default values.

So, you define explicit your mandatory and your optional values in the declaration tag of your fragment.

Here is simple example, with 1 mandatory and 2 optional parameters:

<div th:fragment="printGreetings (name)" th:with="title=${title} ?: 'Mr.', greeting=${greeting} ?: 'Hello'">     <span th:text="${greeting}">Hello</span>     <span th:text="${title}">Mr.</span>     <span th:text="${name}">John Doe</span> </div> 

You can then call it like the following:

<div th:replace="fragments :: printGreetings (name='daniel')">    Hello Mr. Daniel </div> <div th:replace="fragments :: printGreetings (name='Lea', title='Ms.')>    Hello Ms. Lea </div> <div th:replace="fragments :: printGreetings (name='Lea', title='Ms.', greeting='Hi')>    Hi Ms. Lea </div> 

(Please note that all values inside the tags are replaced by the dynamic ones. It's just for better reading.)

like image 43
snap Avatar answered Oct 03 '22 01:10

snap