Reputation: 995
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?
Upvotes: 54
Views: 64980
Reputation: 5244
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.
Upvotes: 68
Reputation: 21
The best way for me :
Fragment:
<div th:fragment="my_fragment(content)">
<p th:text="${content}"></p>
</div>
if content is given from model:
<div th:replace="fragments :: my_fragment(${content})></div>
else:
<div th:replace="fragments :: my_fragment('content')></div>
Upvotes: 1
Reputation: 1915
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.)
Upvotes: 46
Reputation: 657
If you want to provide a default value (as in your case) you can use the elvis operator to specify a default value, for example:
<div th:fragment="my_fragment">
<p th:text="${content}"></p>
<p th:text="${defaultParameter ?: 'the backwards compat value'}"></p>
</div>
see: elvis-operator
Upvotes: 8
Reputation: 18000
At last I've solved task like this:
<div th:fragment="my_fragment2(content, param2)">
<th:block th:replace="my_fragment(${content})"/>
</div>
<div th:fragment="my_fragment(content)" th:with="param2=${param2} ?: 'default value'">
<p th:text="${content}"></p>
<p th:text="${param2}"></p>
</div>
<div th:replace="fragments :: my_fragment2('test', 'my_value')"></div>
But it is overhead:(
Also ask a question to issue Allow fragment parameters to have default values
Upvotes: 3