Thymleaf I18N Syntax - thymeleaf

I have this code;
<div class="row">
<h5 th:text="#{heading.st}"/>
<h6 th:text="${'Sub Type: ' + results[0].subType + '; Internal Switch Role: ' + results[0].internalSwitch}"></h6>
</div>
where #{heading.st} is the internationalized string for 'Sub Type' stored in a properties file. So I can see than i18n is correctly set up. However, I cannot workout the syntax for replacing the string Sub Type in the h6 element. Both
<h6 th:text="${#heading.st + ': ' + results[0].subType + '; Internal Switch Role: ' + results[0].internalSwitch}"></h6>
and
give syntax errors. Can someone point me in the right direction please.

There are more options as well. Personally, my favorite is either using literal substitutions:
<h6 th:text="|#{heading.st}: ${results[0].subType}|" />
or using additional tags like this:
<h6>
<span th:text="#{heading.st}" />: <span th:text="${results[0].subType}" />
</h6>
I think either option is more readable than string concatenation.

Got it!
<h6 th:text="#{heading.st} + ': ' + ${results[0].subType}"></h6>

Related

thymeleaf evaluate string as variable

suppose I have a thymeleaf fragment named "reference" that takes a parameter referenceNumber="1" and in my model I have "reference1_firstName" = "Bob"
<div ... th:fragment="reference(referenceNumber)">
Reference <div th:text="${referenceNumber}"/> first name is <div th:text="${'reference' + referenceNumber + '_firstName'}".>
</div>
in the obviously incorrect example above I would like to print out "Reference 1 first name is Bob". It seems so simple, I can do it in several other languages, but so far my search has come up empty for thymeleaf
Couple ways to do this.
Preprocessing:
<span th:text="${reference__${referenceNumber}___firstName}" />
#ctx basic object:
<span th:text="${#ctx.getVariable('reference' + referenceNumber + '_firstName')}" />
<span th:text="${#ctx['reference' + referenceNumber + '_firstName']}" />
Or if you intend to access variables this way, use a Map instead of variables.

Thymeleaf - How to write Html string literals?

How can we write HTML and string literals at a same time in Thymeleaf ?
<div class="details"><span class="Section" th:utext="'Sec' <br> ${wind.sec}"></span><span class="Axiom" th:utext="'Axiom' <br> ${wind.axiom}"></span></div>
This throws error
Cannot execute GREATER THAN from Expression "('Sec' < br) > ${wind.sec}". Left is "true", right is "Great"
You can use the following = which uses + for string concatenation:
<div class="details">
<span class="Section" th:utext="'Sec<br>' + ${wind.sec}"></span>
<span class="Axiom" th:utext="'Axiom<br>' + ${wind.axiom}"></span>
</div>
The <br> is just part of the text literal in this case - because you are using th:utext.
However, using unescaped values such as ${wind.sec} is unsafe and is not recommended. There could be harmful values in that variable - especially if the variable holds data provided by end users.
So, unless the following structure change poses a problem, I would recommend something like this:
<div class="details">
<span class="Section" th:utext="'Sec<br>'"></span>
<span class="Section" th:text="${wind.sec}"></span>
<span class="Axiom" th:utext="'Axiom<br>'"></span>
<span class="Axiom" th:text="${wind.axiom}"></span>
</div>
Here we have separated the true text literals (which can use th:utext) from the variables (which should use th:text). Now, any HTML which may have found its way into your ${...} variables will be escaped, rendering it harmless.

Thymeleaf - how to add a Span to a dynamic text in a <h1> section

We have a title for our page that we are building dynamically with Thymeleaf
<h1 th:text="'My name is' + ${person.name} + ' ' +${person.lastname}" ></h1>
And the outcome is:
<h1>My name is Inigo Montoya</h1>
Now, we like to color in red the name of the user.
In simple HTML + CSS we can do it like that:
span{
color: red;
}
<h1>My name is <span>Inigo</span> Montoya</h1>
But how to do it correctly in Thymeleaf?
Something like this?
<h1>
My name is <span th:text="${person.name}" /> [[${person.lastname}]]
</h1>

How to format currency input in RoR + AngularJS app

Sorry for my English.
I need to change input value format, for example: from "1000000" to "1 000 000 $".
In my views of rails app, I have this line of code:
<%= ng_text_field('total_price', 'selected.data.total_price', 'Full price', ng_readonly: '!selected.permissions.update') %>
In helper:
def ng_text_field(name, ng_model, placeholder, options = {})
result = <<-HTML
<div class="form-group" ng-class='{"has-error":errors.#{name}}' #{options[:ng_if] && "ng-if=\"#{options[:ng_if]}\""}>
<label for="#{name}" class="col-sm-3 control-label">#{placeholder}</label>
<div class="col-sm-9">
<input id="#{name}"
type="text"
class="form-control"
name="#{name}"
placeholder="#{placeholder}"
ng-model="#{ng_model}"
#{options[:ng_readonly] && "ng-readonly=\"#{options[:ng_readonly]}\""}>
<p class="help-block small" ng-if="errors.#{name}">{{errors.#{name} | join:',' }}</p>
</div>
</div>
HTML
result.html_safe
end
I am know Angular very little, I have tried some ways and all this ways was incorrect. :(
Could anyone give advice of some help?
Thank you in advance
You're going to need to create a new directive that requires ngModel and applies the appropriate $parser/$formatter to it.
https://docs.angularjs.org/api/ng/type/ngModel.NgModelController#$parsers
A good example of how to do this is (displaying as uppercase but always storing data as lowercase):
ngModel Formatters and Parsers
You should be able to then add the ability to include other directives in your 'options' argument so that they get added correctly to the output.

Thymeleaf-Not able to append '/' in html

<span th:each="entry: ${productOptionDisplayValues}">
<span th:if="${entry?.key != 'OfferStatus'}">
<span th:if="${entry?.key=='color_'}" th:text="${entry.value}+ ' / '"/>
<span th:unless="${entry?.key=='color_'}" th:text="${entry.value}"/>
</span>
</span>
I have the following span tag in my html where i am iterating on a map and printing the key value.
At the end of first if block , after printing the value i want to appand a '/' for which i am appending + ' / ' at the end of first if block.
But it's appearing as '아이보리 T38' where instead of / , T is getting appended after the color variance.
It should come as '아이보리 / 38'
If you want Thymeleaf to respect your XHTML or HTML5 tags and not escape them,
You will have to use a different attribute: th:utext (for "unescaped text").

Resources