question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow fragment parameters to have default values

See original GitHub issue

It would be great if fragment parameters could have default values:

<div th:fragment="frag (param = 'hello')">

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:4
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
danielfernandezcommented, Feb 20, 2018

@git2snap the idea is that, at the fragment side, a th:with can assign a default value to a parameter that is not given it (or is null).

Note that parameters appearing in the signature (frag(par1,par2)) are required, so we would have two options:

First option:

<div th:fragment="frag (one)" th:with="two=${two} ?: 'default'">...</div>

Which we could call in several ways (note using named parameters as in the last line we can include parameters that are not in the signature):

<div th:replace="::frag(${onevalue})">...</div>
<div th:replace="::frag(one=${onevalue})">...</div>
<div th:replace="::frag(one=${onevalue},two=${twovalue})">...</div>

Second option:

<div th:fragment="frag (one,two)" th:with="two=${two} ?: 'default'">...</div>

Which requires us to specify both parameters (nulling the second one if needed):

<div th:replace="::frag(${onevalue},${twovalue})">...</div>
<div th:replace="::frag(${onevalue},${null})">...</div>
<div th:replace="::frag(one=${onevalue},two=${twovalue})">...</div>
<div th:replace="::frag(one=${onevalue},two=${null})">...</div>
2reactions
danielfernandezcommented, Sep 10, 2017

@JavaWebinar please note that th:replace has greater precedence than th:with, so in your tag:

<div th:replace="fragments::headTag('title')" th:with="img='value'">

That th:replace will execute, replace the tag, and therefore make that th:with simply disappear. So it will never execute and your img local variable will be never given a value.

If you want to specify that additional parameter, you can do:

<div th:replace="fragments::headTag(title='title',img='value')">
Read more comments on GitHub >

github_iconTop Results From Across the Web

Thymeleaf, fragments and default parameters - Stack Overflow
The best way to allow optional parameter for a fragment is to declare them with "th:with" and describe them with meaningful default values....
Read more >
SafeArgs: partial default values in an action produce wrong ...
We should always put arguments without a default value before those with a default value, following the order on the destination itself within...
Read more >
Default parameters - JavaScript - MDN Web Docs
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Read more >
Pass data between destinations - Android Developers
Destination-level arguments and default values are used by all actions that navigate to the destination. If needed, you can override the default ......
Read more >
Variables - Relay
GraphQL variables are a construct that allows referencing dynamic values ... Fragments can also reference variables that have been declared by a query:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found