Dynamic Thymeleaf fragment location - thymeleaf

Imagine a tag:
<div th:replace="pageName/page.html :: fragmentName" />
where pageName should come from model like ${page.name}, but I don't know the syntax how I can insert a variable to the beginning of the fragment location.
Any ideas?

According to this, you can simply use variables in fragment expressions. (The example given is ~{${templateName} :: ${fragmentName}}. This should work for you:
<div th:replace="~{|pageName/${page.name}.html| :: fragmentName}" />

Related

Include javascript code in Thymeleaf on demand

is it possible to use thymyleaf conditions for building javascript function ?
For example I have a flag specialClient which I will pass from java code to template render engine. So now I want to write something like followed code in my template:
...
<th:if=${specialClient}>
callbackForSpecialCLient()
<else>
plainCallbackWithAdForPoorClients
<endif>
...
which (after rendering) should result in:
...
callbackForSpectialClient()
...
As you can see we don't have any if conditions in rendered result. That's what I want so much to achieve.
You could achieve your desired functionality with following code:
<script th:inline="javascript">
<th:block th:if="${specialClient}">
callbackForSpecialCLient();
</th:block>
<th:block th:unless="${specialClient}">
plainCallbackWithAdForPoorClients();
</th:block>
</script>

Adding a conditional in gsp file

I have a situation where I get a value as a string and want to do a <= check on it inside my Grails GSP file.
For example.
<g:set var="dueAmount" value="${bean.dueAmount}"/>
<span class="pay-onetime-btn-wrapper ${dueAmount <=0 ?'show':'hide'}" >bla bla </span>
I get the following error.
java.lang.Integer cannot be cast to java.lang.String
Which makes sense as the bean.dueAmount is a string. How can I format it as a number or be able to a <= value comparison on it?
Thanks
Looks like your value is a double or floating value. So you can use toDouble() instead. Also, call toString() for the safe side before it.
<g:set var="dueAmount" value="${bean.dueAmount.toString().toDouble()}" />
<span class="pay-onetime-btn-wrapper ${dueAmount <=0 ? 'show' : 'hide'}">bla bla</span>
Grails has a toInteger() function
<g:set var="dueAmount" value="${bean.dueAmount.toInteger()}"/>
<span class="pay-onetime-btn-wrapper ${dueAmount <=0 ?'show':'hide'}" >bla bla </span>

How to Capture html tags using lua pattern

This is how what i'm trying to extract from looks : http://pastebin.com/VD0K3ZcN
lines:match([[title="(value here)">]])
How can I get the "value here"? it does not have numbers or the ">" symbol inside it, only letters, spaces, ' - and .
I have tried
lines:match([[title="(.+)">]])
but it simply got the whole line after the capture.
The problem with your pattern is this:
title=" -- This is fine, but you probably want to find out what tag title is in.
(.+) -- Problem: Greedy match. I'll illustrate this later.
"> -- Will match a closing tag with a double quote.
Now, if I have this HTML:
<html>
<head title="Foobar">
</head>
<body onload="somejs();">
</body>
</html>
Your pattern will match:
Foobar"></head><body onload="somejs();
You can fix this by using (.-). This is the non-greedy version, and it will match the least amount possible, stopping once it finds the next "> instead of the last ">.

Counters in Loops in Thymeleaf

Is there a way to do a loop in Thymeleaf without a list?
I'd like to essentially convert this snippet to Thymeleaf:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<c:forEach var="i" begin="0" end="99">
<form:option value="${year-i}" />
</c:forEach>
</form:select>
-- Update --
I've decided this is along the lines of how I want to do it, but I'm not sure about the springEL syntax:
<option th:each="i : ${#numbers.sequence( 1, 100)}" th:value="#{ T(java.util.Date).getYear() - $i }">1</option>
In case you are still looking for the correct SpEL syntax,
here's what worked for me:
<option th:each="i : ${#numbers.sequence( 1, 100)}"
th:value="${ (new org.joda.time.DateTime()).getYear() - i }"
th:text="${ (new org.joda.time.DateTime()).getYear() - i }">1</option>
Notice:
added th:text to set the option text.
used Joda-Time instead as java.util.Date wouldn't give me the desired outcome
Read this discussion on java.util.Date and getYear()
You can use the special thymleaf iteration variable inside the each block.
This special variable name is the name of your element variable concatenate with the keyword 'Stat' (ex: elt -> eltStat)
This variable gives you many information related to the iteration.
You can also specify this variable name after your element variable. For example:
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
More information in the official documentation below:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

Grails GSP <g:set> tag set as integer?

Using Grails' GSP <g:set> tag, is it possible to specify the type of the variable? I want to declare an integer variable, but <g:set> always declares a sting. For example:
<g:set var="x" value="100"/>
${x.getClass()}
${x+23}
results in
class java.lang.String
10023
I'd like to declare x as an integer. I noticed that using the JSP tag <% int x=100; %> results in:
class java.lang.Integer
123
Is there a way to do this the Grails/GSP way?
Use the ${} syntax when defining the value. For example:
<g:set var="x" value="${100}"/>
You can see the tag doc for g:set for more info.
Just as an additional comment for someone who comes across this since it is the only useful result on the Internet for and casting/Int/Sring/etc. This example works in the case of variables:
<g:set var="printLeft" value="${offer?.metaInfo?.redeemPrintY as Integer}"/>
<g:set var="printTop" value="${offer?.metaInfo?.redeemPrintX as Integer}"/>
<g:set var="printWidth" value="${offer?.metaInfo?.redeemPrintW as Integer}"/>
<g:set var="printHeight" value="${offer?.metaInfo?.redeemPrintH as Integer}"/>
...
<area shape="rect" coords="${printLeft},${printTop},${printLeft+printWidth},${printTop+printHeight}" onClick="printOffer();" />

Resources