Thymeleaf variables scope global - thymeleaf

I want to define a Thymeleaf variable in the body tag so it is visible in all tags nested within the body.
But I want to assign a value in the form tag.
I tried:
<body style="padding-top: 65px;" th:with="isOwner=false">
<form th:with="isOwner=true">
<div th:text="${isOwner}"></div>
</form>
<div th:text="${isOwner}"></div>
</body>
Unfortunately this resulted in true false. Thus it seems that there are two variables.
How do I solve this problem?

You declare a variable using th:with. You are doing it once in body and again in form. Remove the th:with in the form
Update: These are local variables whose scope is between the tags.

Related

Evaluating expressions in thymeleaf fragment

I am trying to evaluate an expression that comes from a thymeleaf fragment. But it seems everything is just converted to a string.
Here is what I have in my main template
[(~{text:default_voucher::voucher-code})]
And voucher-code fragment looks like
<th:block th:fragment="voucher-code">${Voucher_code}</th:block>
But the value of Voucher_code does get shown. Instead just the text ${Voucher_code} gets shown.
In my main template if refer the variable there instead of using the fragment, the voucher code shows up.
[[${Voucher_code}]]
Is there a way to get this to work?
Update
I got it working by changing the main template to include the fragment like
<th:block th:insert="~{text:default_voucher::voucher-code}" />
You don't display a value like this in Thymeleaf:
<th:block th:fragment="voucher-code">${Voucher_code}</th:block>
Thymeleaf evaluates expressions inside attributes of tags.
Therefore, it should be something like this:
<th:block th:fragment="voucher-code">
<p th:text="${Voucher_code}"></p>
</th:block>

How to use a razor variable mixed with html ID text?

I am getting an error because the razor and html is getting confused by the compiler I imagine.
<div id="#Model.MyLabelCars" ...>
My model variable is:
Model.MyLabel
The "Cars" is just raw text that should be in the HTML.
So say Model.MyLabel's value is "123" the ID should be:
id="123Car"
How can I seperate the model's variable name and HTML?
You could use regular string add operator
<div id="#(Model.MyLabel + "Car")"></div>
Or C# 6's string interpolation.
<div id="#($"{Model.MyLabel}Car")"></div>
What you want is to use the <text></text> pseudo tags
<div id="#Model.MyLabel<text>Cars</text>" ...>

Part of Thymeleaf fragment included only once

I have a Thymeleaf fragment like this
<div th:fragment="f1">
<script src="x.js"></script>
<div> ... </div>
</div>
The script part I want to include it only once, even though I will include f1 more than once in the page.
What's the easiest/cleanest way to achieve this?
I can even split this fragment into two fragments and then the problem will be reduced on how to include a fragment only once in a page.
It can be somehow done if we'll be able to manipulate the request attributes from Thymeleaf. I understand that Thymeleaf if for rendering only, but a feature like this could be useful. Even to have something like include-once or if the included fragment has an id(or th:id) attribute, to include it only once.
use #ids.
<div th:fragment="f1">
<script src="x.js" th:if="${#ids.seq('f1.script.') == 'f1.script.1'}"></script>
hello
</div>
or
<div th:fragment="f1">
<script src="x.js" th:if="${#ids.next('f1.script.') == 'f1.script.1'}" th:id="${#ids.seq('f1.script.')}"></script>
hello
</div>

How to set variables in a thymeleaf fragment?

I understand Thymeleaf is made for rendering the views however, I just wanted to know if there is any way to set a variable may be in request scope in a Thymeleaf fragment?
I have a very large conditional expression that I have to repeat a lot of times across the application so it will be really helpful if I can set a variable in main layout then reuse it in all other child fragments.
I am aware of using a Spring interceptor and setting the variable in model but I prefer not to.
Please advise.
Thanks
Prash
In the fragment template, define fragment with parameters:
<div th:fragment=”myfragment(myvariable)”>
<p th:text=”${myvariable}”></p>
</div>
and in layout template, include fragment with that variable specified:
<div th:include=”template :: myfragment(${variable})”></div>
Then variable is passed to the fragment template.
If you need the result of your expression only in the fragments you could use
th:with="var=${verylargeexpression}"
This creates a local variable which you can use everywhere within the dom element you defined it, including fragments.
<div th:include="'path/your/file/' + ${variable}"></div>
The code above can use variables that you can choose to build the file path
If you are using Spring MVC along with Thymeleaf, then you should be able to access any bean within thymeleaf template.
Just use expressions like this in global template ...
<span th:text="${beans.myBean.verylargeexpression}"></span>

Is there a command to show all fields but selected with Grails Fields Plugin?

Right now, Grails generated the next code:
<f:all bean="forestHappyAnimals"/>
I want to hide/show some fields with Javascript doing something like:
<div id="message-1" onclick="document.getElementById('hideMeId').style.display='none'">Click to hide</div>
<div id="message-2" onclick="document.getElementById('hideMeId').style.display='inline'">Click to show</div>
<div id="hideMeId">
...
</div>
If I write the next code, the property name appear twice:
<f:all bean="forestHappyAnimals"/>
<div id="hideMeId">
<f:field bean="forestHappyAnimals" property="name"/>
</div>
How could I avoid that field repeated? The only way is to write <f:field bean="... for all fields?
Try to use except attribute like describes in documentation
except - A comma-separated list of properties that should be skipped (in addition to the defaults).

Resources