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>
Related
I have a fragment that looks like
<th:block th:fragment="link-url">[[${Subscription_link}]]</th:block>
Subscription_link is a variable.
Now I want to pass the value of fragment to a variable in th:with like
<a th:with="url=~{link-url}"></a>
but this doesnt work as when I try to print the result like
<div>[[link-url/text()}]]</div> I get <TH:BLOCKTH:TEXT="${SUBSCRIPTION_LINK}"></TH:BLOCK>
But when I use <th:block th:insert="~{link-url}" /> the value gets properly evaluated and printed out. But then I cant use this inside a th:with. So kinda stuck.
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>
I frequently use code like:
<p #if(Model.Sth)
{
?:style="display: none;"
}>Some text</p>
Many template engines have special markers for conditional blocks, for example in Mustache you can write:
<p {{#Model.Sth}}style="display: none;"{{/Model.Sth}}>Some text</p>
Can my Razor code can be written in shorter form?
In Razor you can embed expressions inside parenthesis to execute them inline. If your conditional can be written using the ternary operator, then you can do something like this:
#(this.Model.Sth ? "style='display:none;'" : string.Empty)
The trick is getting Razor to emit the resulting string correctly back into your HTML. You could use HtmlHelper to do it, but it gets messy enough that the long-form conditional is much cleaner.
In the specific case of an attribute, however, there's a special feature of Razor, as of MVC4, that will help. If you specify an attribute using an expression that evaluates to null, MVC won't emit the attribute at all, so you can do:
<p style="#(this.Model.Sth ? "display:none;" : null)">Some Text</p>
(Note that null and string.Empty are different in this case: Razor will emit style="" if your expression evaluates to empty string.)
I am trying to evaluate an expression in a GSP tag. Crudely put,
<g:form ${true ? 'name=\"hello\"' : ''}>
But the error I'm getting is:
Class: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
Message: Attribute value must be quoted (${true ? 'name=\"hello\"' : ''}).
In one of the other views, the following expression (which is similar to the above one) works fine.
<li ${controllerName == null ? ' class=active' : ''}>
But it does not work with the form tag. I am using Grails 2.3.5. Any idea what I'm doing wrong?
Edit: I am having similar problems with this expression too.
<g:form url="[${multiple ? '' : 'resource:xyzInstance, '}action:'update']" method="PUT" >
Here, multiple is a boolean value. It works fine with an <g:if> tag.
<g:if test="${multiple}">
In Groovy double quotes must not be escaped inside single quote strings.
The opposite also applies: single quotes must not be escaped inside double quote strings (known as GStrings, by the way)
Try
<g:form ${true ? 'name="hello"' : ''}>
or
<g:form ${true ? "name=\"hello\"" : ''}>
Both will work.
For the second part of the updated question I would recommend to build the map separately as it is not only going to work but your code is going to be easier to read and maintain. Something like:
<%
def urlMap = [action: 'update']
if(multiple)
urlMap.put 'resource', 'xyzInstance'
%>
<g:form url="${urlMap}" method="PUT">
Be careful also with the types you use as values on some attributes, this has caused me some troubles sometimes:
'a String'
"a GString with a ${dynamicValue}"
"[a GString that looks like a list]"
"${[a, list]}"
"${[a: map]}"
"false" <- This will always be evaluated to true, as it's a non-empty non-null GString
"${false}" <- Solution to the problem of the line above
I hope all this solve your problems. For simplicity's sake I'd always recommend to keep value expressions as simple as possible, and if they need complex logic put it in a block immediately before the tag it's gonna benefit from it.
I guess this is not the real expression
<g:form ${true ? 'name=\"hello\"' : ''}>
Because there's no point is using an expression for this, as it's equivalent to:
<g:form name="hello">
I guess what you need is something like
<g:form name="${someVariable ? 'hello' : ''}">
If this doesn't help, could you update your question to show the real expression?
I'm trying to build up a bit of HTML using a mix of razor variables and static content.
Here is where I get stuck: I have a counter variable in my view called section_counter. I want to use that in the ID of the image tag I'm building. However unlike with <% .. %> notation I'm used to, I'm just not able to do what I need.
<img alt="section" id="#section_counter_Section" src=""..... etc
I need the id to look like 3_Section. However if I leave a space between the variable and the word _Section, the value retains that space (3 _Section).
If I use the <text> hint, I get this:
<img alt="section" id="3<text>_Section</text>" src="
In my generated HTML. What am I missing?
Try putting your variable in brackets. (An explicit code nugget)
<img alt="section" id="#(section_counter)_Section" src=""