How to use anchor link or HTML in ternary operator in thymeleaf - spring-boot

I encountered a problem that need to write some HTML codes in ternary operator in Thymeleaf. In where I need to chose an anchor link by using ternary operator. For better understand, I putted those problematic code in below:
<span th:text="${error_code == '404'} ? 'Home' : 'Login'"></span>
So, how can I wirte those code correclty in Themeleaf

This should do it :
<span th:utext="((${error_code} == '404') ? '<a href="http://localhost:8080/home">Home</a>' : '<a href="http://localhost:8080/login">Login</a>')"></span>
" is for escaping double quote in html.
utext is to tell thymeleaf not to print plain text as "< a href.. "

Related

Binding classes issue with AlpineJS - ternary operator and curly brackets

EDIT: Due to the general syntax of AlpineJS, writing a ternary operator inside curly brackets is a mistake you may easily run into.
This is just a "grammar" issue but I really want to figure it out... Let's jump into it.
Everything is ok if I write:
:class="{ 'some_class': activeSlide == slide }"
On the contrary it doesn't work (i.e. 'some_class' is not added as a class) if I write:
:class="{ activeSlide == slide ? 'some_class' : '' }"
What's wrong with it?
(I don't think it's relevant, but you can have a look at the entire code here, inside 'template' tags: link)
The ternary operator works only with the non-object class syntax:
:class="activeSlide == slide ? 'some_class' : ''"
You can use the shorthand conditionals as well:
:class="activeSlide == slide && 'some_class'"

Setting up thymeleaf value to anchor tag and null check

I am setting thymeleaf anchr tag href as below to use query param,
<a th:href="#{'/page?vars='+${param.q1}+'&varr=' + ${param.q2}}">
the above is working fine. But sometimes, I get q1 url param will be null. In that case, I need to set default value.
I have tried like,
<a th:href="#{'/page?vars='+${param.q1 != null} ? ${param.q1} : 'default' +'&varr=' + ${param.q2}}">
But I am getting error on this page. Any suggestions on this?
You should be using Thymeleaf's standard URL syntax when generating links (rather than string concatenation). This expression should work for you:
<a th:href="#{/page(vars=${param.q1} ?: 'default',varr=${param.q2})}">
This should do the trick
<a th:href="#{'/page?vars='+ ${param.q1}?: 'default' +'&varr=' + ${param.q2}}">
Source : https://www.baeldung.com/spring-thymeleaf-conditionals#1-elvis-operator

Xpath insert "," after every li

I have a piece of code and XPath to export it. the code is:
<div class="container-fluid">
<ul class="tags expandable">
<li><a class="search__link" href="domain.com">office</a></li>
<li><a class="search__link" href="domain.com">space</a></li>
</ul>
</div>
and the Xpath is :
//ul[contains(concat (" ", normalize-space(#class), " "), " tags expandable ")]
this Xpath export data is like this: "office space"
but I want to insert "," after each li and I want the export like this: "office, space,"
You should use something like this with XPath 1.0:
translate(normalize-space(//ul)," ",",")
returns "office,space".
To add the last coma :
concat(translate(normalize-space(//ul)," ",","),",")
returns "office,space,".
EDIT : With the website link you posted, use this one-liner to get what you want :
translate(normalize-space(//div[#class="container-fluid"]/ul)," ",",")
Output :
People,Space,Women,Friends,Communication,Group,Support,Community,Beautiful,Unity,Gender,Movement,Gathering,Copy,Rights,Feminist,Empower,Supporting,Copy,space,Empowering
It still needs some corrections (remove the undesired "," between Copy and space). If you need something fully automatic and since you have to use XPath 1.0 (no replace function), you can try :
translate(normalize-space(translate(//div[#class="container-fluid"]/ul," ",""))," ",",")
Output (Copy space are now merged) :
People,Space,Women,Friends,Communication,Group,Support,Community,Beautiful,Unity,Gender,Movement,Gathering,Copy,Rights,Feminist,Empower,Supporting,Copyspace,Empowering
Otherwise, just use :
//ul[#class="tags expandable"]//a/text()
And add the comas with the programming language you want.

How to render HTML when inside {{}} on Brade with ternary operator in Laravel 5.7?

I have the follow code in Blade using a ternary operator:
<td>{{isset($arrTemp[$ccc->id]) ? "<a hfet='".url('/cc/'.$cc->id)."'>".count($arrTemp[$cc->id])."</a>": 'N/A'}}</td>
If it find somenthing for the array key $cc->id, should thisplay the value with the link atteched to it.
But the page is rendering <a hfet='http://my.test/cc/56526235'>4</a> the string itself.
What am I missing?
When you use {{ }} the output is automatically escaped to prevent XSS attacks. You can use {!! !!} instead, which will not escape the string.
Source: https://laravel.com/docs/5.4/blade#displaying-data

I need a regex to find a url which is not inside any html tag or an attribute value of any html tag

I have html contents in following text.
"This is my text to be parsed which contains url
http://someurl.com?param1=foo&params2=bar
<a href="http://thisshouldnotbetampered.com">
some text and a url http://someotherurl.com test 1q2w
</a> <img src="http://someasseturl.com/abc.jpeg"/>
<span>i have a link too http://someurlinsidespan.com?xyz=abc </span>
"
Need a regex that will convert plain urls to hyperlink(without tampering existing hyperlink)
Expected result:
"This is my text to be parsed which contains url
<a href="http://someurl.com?param1=foo&params2=bar">
http://someurl.com?param1=foo&params2=bar</a>
<a href="http://thisshouldnotbetampered.com">
some text and a url http://someotherurl.com test
1q2w </a> <img src="http://someasseturl.com/abc.jpeg"/>
<span>i have a link too http://someurlinsidespan.com?xyz=abc </span> "
Disclaimer: You shouldn't use regex for this task, use an html parser. This is a POC to demonstrate that it's possible if you expect a good formatted HTML (which you won't have anyway).
So here's what I came up with:
(https?:\/\/(?:w{1,3}.)?[^\s]*?(?:\.[a-z]+)+)(?![^<]*?(?:<\/\w+>|\/?>))
What does this mean ?
( : group 1
https? : match http or https
\/\/ : match //
(?:w{1,3}.)? : match optionally w., ww. or www.
[^\s]*? : match anything except whitespace zero or more times ungreedy
(?:\.[a-z]+)+) : match a dot followed by [a-z] character(s), repeat this one or more times
(?! : negative lookahead
[^<]*? : match anything except < zero or more times ungreedy
(?:<\/\w+>|\/?>) : match a closing tag or /> or >
) : end of lookahead
) : end of group 1
regex101 online demo
rubular online demo
Maybe you could do a search-and-replace first to remove the HTML elements. I don't know Ruby, but the regex would be something like /<(\w+).*?>.*?</\1>/. But it might be tricky if you have nested elements of the same type.
Maybe try http://rubular.com/ .. there are some Regex tips helps you get the desired output.
I would do something like this:
require 'nokogiri'
doc = Nokogiri::HTML.fragment <<EOF
This is my text to be parsed which contains url
http://someurl.com <a href="http://thisshouldnotbetampered.com">
some text and a url http://someotherurl.com test 1q2w </a> <img src="http://someasseturl.com/abc.jpeg"/>
EOF
doc.search('*').each{|n| n.replace "\n"}
URI.extract doc.text
#=> ["http://someurl.com"]

Resources