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>
Related
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>
I need an image loaded onto a html img tag using thymeleaf. The problem is, the image itself is obtained from a url which takes in two parameters.
Sample:
<img src="/products/images?categoryId=1&image=1" />
The trouble is, the image parameter is generated dynamically and hence I need to use a thymeleaf expression there. Therefore I tried something like this:
<img th:src="#{'/products/images?categoryId=1&image=' + ${product.id}}" />
But when I run this, I get the following message:
Exception parsing document: template="product-list", line 104 - column 59
Which points to the location where the '&' symbol occurs. Now, I have tried using '& amp;' but then, the url becomes something like
/products/images?categoryId=1&image=1
Obviously, this is not going to work.
So how else do I make a valid link with two parameters using thymeleaf then?
This can easily done by Thymeleaf. Don't concatenate strings and
simply use #{'/products/images'(categoryId=1, image= ${product.id})}
See the documentation.
The way that you escape an ampersand & in any html attribute is &. Actually you should always escape ampersands in all html attributes whether you are using Thymeleaf or not.
See this question for more details and references:
Do I encode ampersands in <a href...>?
In a tag like below:
<a data-url="/new/request/showText/${requestInstance.id}"> view text</a>
Is there a way to not hardcode the url like I have?
I tried using <g:link action="showText" id="${requestInstance.id}"> but that renders to an anchor tag.
What I'm doing might be find but I'm just curious if there is a better way to do this in grails?
You may use
${createLink(action:'myaction',params:[datasetId:dataset.id])}
for full control. It just returns something like http://myapp/myaction/123 and supportsall the params which g:link supports.
To be more specific:
<a data-url="${createLink(action:'showText',id: requestInstance.id)}"> view text</a>
should work.
You can use the createLink function inside of ${..}.
So in your case this would be:
<a data-url="${createLink(controller: 'yourController', action: 'yourAction', params:[param1: 'value1'])}"> view text</a>
I'm trying to create Anchor tags using values in my DB for the name. That way it's easy for me to call them from another page.
<a name="<%= sailing_class.sort_order %>"</a>
But the value from sailing_class.sort_order never appears.
All help is greatly appreciated. I'm on Rails 3.2
You're missing a > (closing <a), so the html is not vaild, and an anchor should be with an id attribute instead of name . Try
<a id="<%= sailing_class.sort_order %>"></a>
I'm learning RAZOR.
I need to make an href tag unique, by adding a letter to the start of the #ref:
eg.
<a href="#p23">
In Razor, to populate the href tag from my model, I have:
<a href="#p#item.ID">
However, Razor doesn't recognise #item.ID, unless it has no characters in front of it.
<a href="#p #item.ID">
But that then invalidates the href.
Is there a way for me to add the letter 'p' to this, and still allow RAZOR to add the ID of the item?
Thank you,
Mark
Just wrap it in parenthesis to aid the parser:
<a href="#p#(item.ID)">
In VB enclose the variable with brackets: "#p#(item.ID)"
in c# it's either the same or use curly brackets.
fixit
just put brackets around the item, i.e.
<a href="#p#(item.ID)">
you can do this across all of the razor syntax where required to mix/match