I have a web application and using Thymeleaf with spring boot, I need to include an option in my javascript in-case the user locale was Arabic, so how add a conditional block and should be processed at server side?
<script th:inline="javascript">
var customerNameTitle = /*[[#{pendingPayments.customerName}]]*/ 'customer Name';
var amountTitle = /*[[#{pendingPayments.amount}]]*/ 'Amount';
var paymentDateTitle = /*[[#{pendingPayments.paymentDate}]]*/ 'payment Date';
var submissionDateTitle = /*[[#{pendingPayments.submissionDate}]]*/ 'submission Date';
$("document").ready(function(e) {
/*<![CDATA[*/
var table = $("#example").DataTable( {
"ajax": {
"url": /*[[@{/payments/getPendingPayments}]]*/ "",
"type": "GET",
"dataSrc": ""
},
"columns": [
{ "data": "customerFullName", "title": customerNameTitle },
{ "data": "amount", "title": amountTitle },
{ "data": "paymentDate", "title": paymentDateTitle },
{ "data": "submissionDate", "title": submissionDateTitle },
],
"language": {
"emptyTable": /*[[#{pendingPayments.emptyTable}]]*/ "",
"url":/*[[@{'/json/dataTables-ar.json'}]]*/ ""
}
});
/*]]>*/
});
</script>
the "url":/*[[@{'/json/dataTables-ar.json'}]]*/
should only be loaded if the locale is Arabic, otherwise the the whole line shouldn't be printed in HTML page..
in JSTL I can do that using <c:if>
<c:if test="${LOCALE.language eq 'ar' }">
....
</c:if>
is there an equivalent in Thymeleaf?
The one way to add CSS and JS files is to add them to the static folder of src/main/resources and reference it to the Thymeleaf template. Here @{} is the way of linking URL in Thymeleaf.
Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates.
Switch Statements Thymeleaf also provides a way to display content conditionally using the equivalent of a switch statement in Java: the th:switch and th:case attributes. Note that as soon as one th:case attribute is evaluated as true , every other th:case attribute in the same switch context is evaluated as false .
Although, a old question, the following worked for us.
<script th:inline="javascript">
/*<![CDATA[*/
var isInlineEdit = [[${param.isInlineEdit} != null ? true:false]];
if(isInlineEdit){
//in line edit code
}else{
//no in line edit
}
/*]]>*/
</script>
The closest I found in Thymeleaf 2 is to add a th:if
condition to the whole <script>
tag. You can then have multiple <script>
tags but of course there will be come copy-pasting involved.
This feature is available in Thymeleaf 3
<script th:inline="javascript">
var customerNameTitle = /*[[#{pendingPayments.customerName}]]*/ 'customer Name';
var amountTitle = /*[[#{pendingPayments.amount}]]*/ 'Amount';
var paymentDateTitle = /*[[#{pendingPayments.paymentDate}]]*/ 'payment Date';
var submissionDateTitle = /*[[#{pendingPayments.submissionDate}]]*/ 'submission Date';
$("document").ready(function(e) {
/*<![CDATA[*/
var table = $("#example").DataTable( {
"ajax": {
// Using textual syntax from Thymeleaf 3
// (not sure about the exact condition for your case
// but this is the syntax to use)
[# th:if="${LOCALE.language.equals('ar') }"]
"url": /*[[@{/payments/getPendingPayments}]]*/ "",
[/]
"type": "GET",
"dataSrc": ""
},
"columns": [
{ "data": "customerFullName", "title": customerNameTitle },
{ "data": "amount", "title": amountTitle },
{ "data": "paymentDate", "title": paymentDateTitle },
{ "data": "submissionDate", "title": submissionDateTitle },
],
"language": {
"emptyTable": /*[[#{pendingPayments.emptyTable}]]*/ "",
"url":/*[[@{'/json/dataTables-ar.json'}]]*/ ""
}
});
/*]]>*/
});
</script>
See the Thymeleaf textual syntax in https://github.com/thymeleaf/thymeleaf/issues/395
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With