Search
 
SCRIPT & CODE EXAMPLE
 

HTML

thymeleaf optional fragment parameter



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.)
Comment

PREVIOUS NEXT
Code Example
Html :: visual studio code black mode off 
Html :: href in spring mvc 
Html :: html5 tutorial 
Html :: nuxt for add attribute select 
Html :: pills vertically with content on right 
Html :: hypertextmarkuplanguage 
Html :: typo3 fluid form input text 
Html :: html textarea placeholder multiple lines 
Html :: how to change html code with aspx.cs 
Html :: <pre; html tag that preserves spacing 
Html :: design college website using html 
Html :: docusign alternative 
Html :: buttosn html 
Html :: react hooks html onchange two functions 
Html :: sublimelinter-html-tidy for linux 
Html :: rails patch method 
Html :: can we manipulate the html elements 
Html :: pavlova 
Html :: html commenting 
Html :: simple landing page html 
Html :: how to make things move in unity 
Html :: common html tags 
Css :: placeholder font size 
Css :: text-overflow ellipsis multiple lines 
Css :: css position absolute middle 
Css :: absolute vertical position css 
Css :: css remove bullet point from ul 
Css :: ul remove dots 
Css :: remove the glow in bootstrap input 
Css :: css elipsis 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =