Thymeleaf String Concatenation - thymeleaf

Thymeleaf String concatenation not work
data-th-text=${employee.empFirstName} +' '+ ${employee.empLastName}
could anyone explain reason for this.
Thanks

You have two options (span is just a example-element):
1.)
<span th:text="${employee.empFirstName + ' ' + employee.empLastName}"></span>
2.) using "|...|"
<span th:text="|${employee.empFirstName} ${employee.empLastName}|"></span>
You are using data-th-text (writting html5 custom attributes). You can use the same syntax here. Have a look at the single- and double-quotes, vertical lines and ${...} blocks.

Related

Make <p> appear and dissapear in Thymeleaf according to variable

Hello I am building an application in Spring Boot and Thymeleaf and I have a paragraph that I want to make it appear only if it has a value. If it does not I do not want it to appear.
Here is the code that I have tried:
<h2 th:text="'Raspuns: ' + ${raspuns}" th:disabled="${raspuns}==null"></h2>
But when I enter the page it says: Raspuns: null I want to make that dissapear.
try this:
<h2 th:if="${raspuns} != null" th:text="'Raspuns: ' + ${raspuns}"></h2>
see more about conditions in the documentation:
Thymeleaf Conditional Evaluation

How can I use a localization files and overwrite 1 localization function

I am trying to build a localized bootstrap-table.
<table class="table table-condensed table-bordered table-hover"
data-cache="false"
data-show-footer="false"
data-show-refresh="false"
data-show-toggle="false"
data-toggle="table"
/>
$('#table').bootstrapTable({
locale :"nl-NL"
});
Everything works when I include the locale-file bootstrap-table-nl-NL.js
I want to overwrite the footer text showing the pagination text. In the documentation it says I need to define a function for formatShowingRows
There seems to be a problem using both locale and formatShowingRows
$('#table').bootstrapTable({
locale :"nl-NL"
formatShowingRows: function (pageFrom, pageTo, totalRows) {
return 'Showing ' + pageFrom + ' to ' + pageTo + ' of ' + totalRows + ' rows';
}
});
When I execute the above script. The pagination text is not replaced with the text I want.
If I remove the locale. It does.
So it looks like locale and formatShowingRows are not compatible.
Any ideas ??
You can use updateFormatText to change the messages BootstrapTable uses even (or especially) with localization. See https://live.bootstrap-table.com/code/wenzhixin/1640 for an example.

Escaping ampersand in custom attribute using Thymeleaf 3?

Is it possible to do basic escaping (of ampersand, in my case) when generating a custom attribute? I have tried many ways (with normal escaping, th:text, th:utext, th:attr for all the dynamic attributes, substituting a custom tag in my Spring code), unfortunately found just some workaround, which is listed below.
Basically, it's about an AngularJS application with a piece of Thymealeaf 3 template:
<script th:inline="javascript">
function customSubmit() {
/*<![CDATA[*/
return /*[(${DIALOG_NAME} + '.$valid && submit()')]*/ null;
/*]]>*/
}
</script>
<form th:name="${DIALOG_NAME}"
th:action= "'/' + ${MODULE} + '.' + ${DIALOG_NAME}"
th:ng-app="${DIALOG_NAME} + 'App'"
th:ng-controller="${DIALOG_NAME} + 'Controller'"
ng-submit="customSubmit()"
...
>
...
</form>
What I am trying to make is one construct like
th:ng-submit="some Thymeleaf expression"
that generates the custom attribute value with the && within:
ng-submit="someDialog.$valid && submit()"
without any function redirection like in the workaround above.
Beside extending Thymeleaf (I am using SpringStandard dialect), is any straightforward way to generate such strings?
Thank you for any suggestion.
Ampersands (&) should be HTML-escaped in tag attributes so you can do it like this:
th:attr="ng-submit='some thymeleaf with && in it'"
EDIT:
If you also want to reference a value of some model attribute in the final form attribute value you have to use the expression preprocessing like this:
<form th:attr="ng-submit='__${DIALOG_NAME}__' + ' some ampersand && in it'">
</form>

#Html.Raw to general javascript function call error if variable with white space

I am new to MVC and Razor engine. Trying to output the below string using #Html.Raw but the expected result is different.
#Html.Raw(" <span class='label label-warning' style='cursor: pointer' onclick=ChangeRPLocStatus(2,1,'1160001','1160001','X','Test 1')>AABBCC</span>")
Output in Html :
<span class="label label-warning" style="cursor: pointer;" onclick="ChangeRPLocStatus(2,1,'1160001','1160001','X','Test" 1')="">AABBCC</span>
The output is different at "Test 1".
I am expecting to get :
ChangeRPLocStatus(2,1,'1160001','1160001','X',Test 1')
but it become :
ChangeRPLocStatus(2,1,'1160001','1160001','X','Test" 1')=""
I am guessing it is because onclick=ChangeRPLocStatus(2,1,'1160001','1160001','X','Test 1') doesn't have quotes around the js method call. Try this?
onclick=\"ChangeRPLocStatus(2,1,'1160001','1160001','X','Test 1')\"
Also, a little tip, did you get the output from the actual source (Ctrl + U in chrome) or via Dev Tools or Firebug, they can be different an cause confusion.

How to insert '_' as htmlAttributes in razor?

This is probably a simple question, but to which I havent found an answer yet.
How to escape the ' _ ' when creating an HtmlElement in razor?
To render a '-' in the final Html we put an ' _ ', but to render an '_' (underscore), How do we escape it? I tryed '#:', but it didn't work, and didn't find any other options...
Example:
#Html.CheckBox("Access_Groups", false, new
{
#class = "input-control checkbox",
#data_group = "I', looking for data-group",
#Description_pt = "<----- I'm looking for Description_pt"
})
#data_group will render as data-group as expected, but...
#Description_pt will render as Description-pt, and that is not what is expected (don't know how to escape the _ for this)
thank you
If you look at the signature of Html.Checkbox, we can see that it takes an object for the htmlAttributes. Further, looking at the syntax, its actually a key based collection of objects. A Dictionary<string,object> fits that bill and allows you to absolutely specify the name of the html attributes that you want to add (note each key is typed exactly how we want it to display).
#Html.CheckBox("Access_Groups", false, new Dictionary<string,object>
{{"class", "input-control checkbox"},
{"data-group", "I', looking for data-group"},
{"Description_pt", "SomeValue"}})
This renders the following HTML
<input Description_pt="SomeValue" class="input-control checkbox"
data-group="I', looking for data-group" id="Access_Groups"
name="Access_Groups" type="checkbox" value="true" />
Have you tried the HTML character code for underscore, i.e., _?

Resources