THYMELEAF - FRAGMENT TAG REMOVED - thymeleaf

I'm trying to replace a tag with a fragment of a template. After the inclusion, some part of the HTML code is removed
In my root page, I have
<p th:replace="mycomponent::firstFragment" th:with="myVar=#{'Hello'}"></p>
in my template I have
<p th:fragment="firstFragment" th:utext="#{someText}"><span th:utext="${myVar}"></span></p>
<p th:fragment="secondFragment" th:utext="#{someOtherText}"><span ></span>${otherVar}</p>
The outcome is only the p element of the fragment without the inside span. I've tried with th:inline="text" but maybe I used it in a wrong way

you can try th:include code. example .....
<span th:include="fragments/navbar::navbarPortion" th:remove="tag"/>

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>

Refer fragment in href or attribute for thymleaf template

is it possible to use a fragment value inside an html attribute like href?
im trying something
<a th:href="~{text:test::text-login}">Tada</a>`
but url comes out like
LOGIN</th:block>">Tada
Im expecting
Tada
I tried out some other syntaxes but got same result
<a th:href="#{{logourl}(logourl=~{text:test::text-login})}">Eureka</a>
You can get it to do exactly what you want by adding /text() to your fragment expression:
<a th:href="~{text:test::text-login/text()}">Tada</a>

Thymeleaf th tag in blc tag

my problem is following:
in this code
<header>
<h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1>
</header>
<blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" />
<ul th:if="${not #lists.isEmpty(menuItems)}">
<li th:each="menuItem : ${menuItems}">
<a th:href="#{${menuItem.url}}" th:class="${menuItemStat.first}? 'home'">
<span th:utext="${menuItem.label}"></span>
</a>
</li>
</ul>
the line <h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1> gets the name of actual menu. I need the menuName in this line <blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" /> to do the same thing, but "${BLC_PAGE.pageFields[menu_name]}" is not appropriate, as it is from thymeleaf tags library. Any idea how to get this this value in blc:menu tag?
In order to resolve the Thymeleaf expression ${BLC_PAGE.pageFields[menu_name]}, you will need to wrap it like so: th:attr="menuName=${BLC_PAGE.pageFields[menu_name]}". Because menuName is not a standard HTML attribute, you won't be able to simply add the Thymeleaf namespace to menuName (i.e., th:menuName), and, without the th: namespace, Thymeleaf doesn't know that it needs to resolve any expressions. The Thymeleaf attr attribute allows you to use non-standard HTML attributes with Thymeleaf. For more information on setting attribute values using Thymeleaf you can review their documentation.
Here are links to the Broadleaf and Thymeleaf documentation on Processors and Dialects:
Thymeleaf: http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html#dialects-and-processors

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>

Add html tags in another html tags attribute with Asp.Net MVC Razor

i want add html tags in another html tags attribute like:
<p title="<strong class="cssClass">bold</strong>">
some text
</p>
how i can do this?
Only with encode:
<p title="#HttpUtility.HtmlEncode("<strong class=\"cssClass\">bold</strong>")">
some text
</p>
but you receive:
<strong class="cssClass">bold</strong>
At the Browser, title has a default default behavior and you cannot change this on native way. If you want to format the Title property to do a stylish tooltip, you could use a jQuery plugin to do that. Check this link. It shows lots of plugins to do this work.

Resources