<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").
Related
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.
how to apply a ternary condition that Elvis Operator in Thymeleaf.
I tried this but there is an error showing in IDE. the error Tag start is not closed
<div th:utext="${ed.aurl} ? '<button th:href="#{ed.aurl}" target="_blank" download>View Attachment </button>' : 'No Attachment' "></div>
You shouldn't/can't use HTML in attributes like that (it's expecting a valid HTML doc)... why not write this as regular HTML?
<div>
<button th:if="${ed.aurl}" th:href="#{ed.aurl}" target="_blank" download>View Attachment</button>
<span th:unless="${ed.aurl}">No Attachment</span>
</div>
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.
I want to retrieve the value of Test Services code
<p>
<span class="floatLeft w30p">Test Services code:</span>
<span> <strong>F603YPW</strong> </span>
</p>
The rest of the page will really affect what selector(s) you would need to use to get that text, however given only the HTML specified you can use the css adjacent sibling selector to get the element
value = find(:css, 'span.floatLeft.w30p + span').text
if there are a bunch of other elements with the floatLeft and w30p classes then you could get more complicated using xpath selectors and do something along the lines of
value = find(:xpath, XPath.descendant(:span)[XPath.string.n.is('Test Services code:')].next_sibling).text
or with multiple finds
value = find(:css, 'span', text: 'Test Services code:').find(:xpath, XPath.next_sibling).text
If you are able to edit the HTML to
<p>
<span class="floatLeft w30p">Test Services:</span>
<span class="your_class"> <strong>F603YPW</strong> </span>
</p>
(so by adding a class to the span),
you will be able to find its value doing
value = page.find('.your_class').text
If you are not able to edit the HTML, do
value = find(:css, 'the css selector').text
I want to select the first sibling of a tag from an HTML element. Lets say I want to get the <a> that follows this <span>:
<span id="unique"></span>
<a href="#">
This can easily be done with the following Xpath:
//div[#id="unique"]/following-sibling::a
This, however, is very specific in a way that it looks for an <a> tag. How can I make it more generic that it would find any tag as long as it is the first sibling to the element I selected?
Write as below
//span[#id="unique"]/following-sibling::*[1]
Here is a sample HTML :
<div>
<span id="unique"></span>
<p>foo</p>
<p>bar</p>
</div>
XPATH expression:
//span[#id="unique"]/following-sibling::*[1]
output
<p>foo</p>