Thymeleaf check if variable equals a number - thymeleaf

I have the following Thymeleaf code, I want "--" to be printed if taxHistory.landValue equals to zero, and the actual value to be printed if taxHistory.landValue is greater than 0
<td th:if"${taxHistory.landValue==0}" th:text="--"></td>
<td th:unless="${taxHistory.landValue>0}" th:text="|${taxHistory.landValue}|"></td>
However I'm getting the following error
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "0}"
What is the correct way of doing this in Thymeleaf?

There are multiple ways to do this, but you can use a ternary operator and print your string if your desired condition does not match:
<td th:text="${taxHistory.landValue > 0 ? taxHistory.landValue : '--' }">[taxHistory.landValue]</td>
You can use the > notation to keep the HTML formed properly. This assumes that a negative value would also print your string (you didn't mention behavior in that unusual case).

Related

Thymeleaf results in error when using variable inside an expression

For the provided string in my model, I want to print a "growing" list of substrings. For example, if the given string is Wine, the result should be:
W
Wi
Win
Wine
I've come up with the following construct:
<h1 th:with="len=${#strings.length(name)}" th:each="i : ${#numbers.sequence(1, ${len})}" th:text="${#strings.substring(name, 0, i)}"></h1>
but it doesn't work.
If I change ${#numbers.sequence(1, ${len})} to ${#numbers.sequence(1, 4)} it works. However, this way I'd need to manually change the code whenever my initial string's length changes.
I've tried with messages.msg() alternative, but no luck either.
Error:
An error happened during template parsing (template: "class path resource [templates/name-list.html]")
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: "#numbers.sequence(1, ${len})" (template: "name-list" - line 7, col 53)
What am I doing wrong?
th:each has a higher attribute precedence than th:with. Since th:each is evaluated first, the len variable is not available here.
In most cases, you should not/cannot nest ${...} expressions. If the len variable was available here, you should have used len instead of ${len}.
.
<h1
th:each="i : ${#numbers.sequence(1, #strings.length(name))}"
th:text="${#strings.substring(name, 0, i)}" />

GPath Expression: Use HTML element's value as argument

I have this gpath expression
<g:findAll in="${paymentList}" expr="it.account == 10">
and this field
<g:hiddenField name="acct_id"/>
acct_id already has a value and I want to use that value for comparison instead of just putting a static number like 10. How do I do it?
Though first question is, from where did the value for hidden field came?
If it's known or even coming from request or in model, then we can do it like below:
Reason for using ${} in g:set tag is to make the value Integer.
Reason for using ${} in g:findAll tag's expr attribute is to make the value available to expr(Note: it's a string expression which gets evaluated later in taglib).
Hope it helps!

Understanding Ruby on Heroku 'config vars' example

In Getting Started with Ruby on Heroku, the following example is given as a use of config vars:
<h1>Getting Started with Ruby</h1>
<p>
Welcome!
</p>
<% for i in 0..(ENV['TIMES'] ? ENV['TIMES'].to_i : 2) do %>
<p>Hello World #<%= i %>!</p>
<% end %>
This method is supposed to run for the number of the TIMES environmental variable, but it doesn't make sense according to my understanding.
I understand the ruby syntax
truthy_or_falsey_value ? evaluates_if_truthy : evaluates_if_falsey
but here we have something that looks similar in syntax but can't be the same thing semantically. Would someone who understands please explain what's going on? Many thanks!
It's called a ternary operator and you're right that these two are equivalent:
ENV['TIMES'] ? ENV['TIMES'].to_i : 2
truthy_or_falsey_value ? evaluates_if_truthy : evaluates_if_falsey
So what's happening is that the ternary operator is used to populate the last argument of a Range:
If `ENV['TIMES'] is nil then the for loop will be:
for i in 0..2
If ENV['TIMES'] is set to 50, the the for loop will be:
for i in 0..50
I have no experience with Heroku but what I understand from that statement is this:
First you have a for loop which will loop from 0 to a specific value. Then you have a conditional statement which, if true, will return ENV['TIMES'].to_i (the .to_i converts the return value of ENV['TIMES'] to an integer), otherwise it will result in 2. Now if you combine both the conditional and the for loop you can figure out what is going on.
If ENV['TIMES'] evaluates to true then the range of the for loop will be from 0 to the value returned from ENV['TIMES'].to_i, otherwise the range will be from 0 to 2. So the final result will be "Hello World" being repeated i times, dependent on whether or not ENV['TIMES'] exists. I am new to Ruby as well and not very familiar with the syntax but hey, that is what I make of it.

MVC renders Double as empty string

Within a view I have the following code:
#foreach (var ce in Model.MyObjectCollection)
{
<p>
Target: #ce.Target.ToString("#.##")%
<br/>
Level: #ce.Level.ToString("#.##")%
</p>
}
If either the value of Target or Level is 0.00, then MVC simply renders that out as an empty string. Debugging shows the value of each to be 0.00.
I need zero values to render as 0.00%.
Can anyone advise if I'm doing something wrong?
The # format specifier won't display zeros
Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.
You can use the 0 specifier instead
ToString("0.00")%
Use zeros instead of #:
#ce.Target.ToString("0.00")%

throwing error when trying to access hashmap with key with a numeric value or special chars

throwing an error when trying to access hashmap with key with a numeric value or special chars
Here is the code I am trying to use:
<div th:include="${myMap[__${dept.code}__]}"/>
If code has letters , this works fine, but if it holds only a numeric value "1234" , this fails .
Appreciate any resolution on this. Thanks..
If the map is based on string keys you shpuld ensure that the precomputed expression is always a string.
A TextLiteral expression can only consist of a limited type of characters. A-z, underscores, minus and some others.
To ensure it's always a string you can wrap the precomputed expression in single quotes:
<div th:include="${myMap['__${dept.code}__']}"/>

Resources