Difference between th:text and th:value in Thymeleaf - thymeleaf

I just recently started using Thymeleaf through one of my projects. I have seen few examples where th:text=${example} is being used in some places th:value=${example}.
I have gone through the Thymeleaf documentation but couldn't find anything explicitly citing the difference, nor did any question on SO.
Any help would be really appreciated! Thanks.

th:value is modification of html attribute value.
For button, input and option elements, the value attribute specifies the initial value of the element
th:text is used for tag body modification.
div{background-color: lightblue; padding: 2px} // to highlight empty div
<!--th code: <div th:value="${value}"/></div> -->
<br/>Result th:value div: <div value="sometext"/></div>
<!--th code: <form><input th:value="${value}"/></form>-->
<br/>Result th:value form: <form><input value="sometext"></form>
<!--th code: <div th:text="${value}"></div>
Same as: <div>[[${value}]]</div> -->
<br/>Result th:text div: <div>sometext</div>
Here is docs of different Thymeleaf attributes features

Lets see an example:
<input type="radio" name="gender" value="male"> Male<br>
if we want to use thymeleaf in value portion of this input tag, then we will use,
<input type="radio" name="gender" th:value="${someValue}"> Male<br>
if we want to see the text (here Male) sent from the controller dynamically, then we use,
<input type="radio" name="gender" th:text="${someText}""> <br>

th:name => This would be the name of the value that you will be passing to another page (Exemplar scenario).
th:value => This would be the actual value that you would be passing. It could be obtained from a model or straight from the database explicitly.
<form th:action="#{confirm-pass-details.html}">
<button type="submit" th:name="event-id" th:value="${event.get().getEventid()}">Buy Passes</button>
</form>

Related

Comparing Strings in Grails GSP

I'm stumped by this problem. I'm trying to display a class if a certain string is equals to the logged in username. However it doesn't seem to ever evaluate to true.
Here's the code in gsp
<g:if test="${it.from.username == sec.loggedInUserInfo(field: 'username')}">
<div class="direct-chat-msg right">
</g:if>
<g:else>
<div class="direct-chat-msg">
</g:else>
I also tried using this method
<div class="direct-chat-msg ${(it.from.username == sec.loggedInUserInfo(field: 'username')) ? 'right' : ''}">
However nothing I do can get the 'right' class to show up in the div.
Just for good measure, I printed out the values of both classes in my gsp in hidden fields.
<input type="hidden" value="${it.from.username}"/>
<input type="hidden" value="${sec.loggedInUserInfo(field: 'username')}"/>
And the values are exactly the same
<input type="hidden" value="u***#gmail.com">
<input type="hidden" value="u***#gmail.com">
I've tried several combinations of string comparison
.equals(), calling .toString() on both, also trying as String. however nothing seems to be working.
What could the issue be?
I have tried passing the logged in user object in the Model from the controller, and just calling loggedInUser.username and it works. So my question now is, what kind of object is returned by spring security?
The result of sec.loggedInUserInfo(field: 'username')} is an HTML encoded string. Spring security calls encodeAsHTML() before returning 'username' value. Visually they look the same but are they equal? Apparently not!

How to add specific attribute (like "multiple") to Thymeleaf templates?

I have this:
<input id="fileupload" type="file" name="files[]" data-url="rest/controller/upload" multiple>
and I need to make it work in a Thymeleaf template. I have the data-url part figured out but I keep getting an error on the word "multiple". This is needed to allow multiple selection in the file selection window.
I have looked everywhere and have not come across an answer.
EDIT:
If you are not familiar, here is the "multiple" attribute.
http://www.w3schools.com/tags/att_input_multiple.asp
The Standard Dialect of Thymeleaf includes attributes that allow you to set these attributes by evaluating a condition, so that if evaluated to true, the attribute will be set to its fixed value, and if evaluated to false, the attribute will not be set:
e.g for checkedattribute:
<input type="checkbox" name="active" th:checked="${user.active}" />
you have to use:
th:multiple
e.g
<input id="fileupload" type="file" name="files[]" th:multiple="{condition}">
see a tutorial here.

How to set radio button as checked in dart polymer component

I need to show green radio buttons as checked based on model value. HTML has two input elements of type radio:
<input name="color" type="radio" id="red" value="red"></label>
<input name="color" type="radio" id="green" value="green" checked>
Dart code has a variable colorValue='green'. How can the checked attribute of input element be set based on dart variable? Can this be done in HTML or this must be done in dart code?
<input ... checked?="{{colorValue=='green'}}">

JQuery Mobile - Put a data-icon in addition to the checkbox in an input checkbox

I would like to put a nice little data-icon="star" on some chekbox labels of a group of checkboxes. Is there a way to do so using jQuery data-icon or should I just do it directly in the CSS (in which case how do I get the same rendering as the one of data-icon?).
In a dream world, here is what would work:
<div data-role='fieldcontain' data-mini='true'>
<fieldset data-role='controlgroup'>
<input type='checkbox' name='cb1' id='cb1' />
<label for='cb1' data-icon='star'>special</label>
<input type='checkbox' name='cb2' id='cb2' />
<label for='cb2'>normal</label>
</fieldset></div>
Thanks a lot for your help
You would have to use your own custom CSS or JS to do this, data-icon only works with a tags per the documentation: http://jquerymobile.com/test/docs/buttons/buttons-icons.html
This is even more tricky because the icon's are entered as styled spans which is exactly how the jqm checkbox is rendered - they both use the ui-icon for some base styling and positioning.
If you tried to use a jquery function to append it after the jqm styling has worked its magic like so: http://jsfiddle.net/shanabus/zt7cP/1/ - but again, the styling conflicts with the actual checkbox, but the functionality of the checkbox is still there.
Hope this gets you going in the right direction.
I am not sure if this is only available in the latest jQuery mobile (1.4.5) but you can just add ui-icon via the "class" attribute. So for this example it would look like this:
<div data-role='fieldcontain' data-mini='true'>
<fieldset data-role='controlgroup'>
<input type='checkbox' name='cb1' id='cb1' />
<label for='cb1' class='ui-icon-star'>special</label>
<input type='checkbox' name='cb2' id='cb2' />
<label for='cb2'>normal</label>
</fieldset></div>

Why isn't Razor recognizing a variable without wrapping it in parenthesis in some cases?

I have a section of Razor code in one of my views that isn't behaving the way I expect. The code in question contains multiple locations where I need to append the value of a variable from the Model onto an html attribute. The code below is a slightly simplified version of the markup on my page (I've removed some of the markup 'noise' that wasn't important to this example and just made the code harder to read).
There are three instances where I use #topic.StandardTopicId. The 1st and 2nd instances get replaced as I expected, but the 3rd instance name="IsCovered_#topic.StandardTopicId" renders without replacing the variable.
Code in question:
#foreach(var topic in Model.Group) {
<div id="frame-#topic.StandardTopicId" class="topic-frame">
<label>
<input type="radio" name="IsCovered_#(topic.StandardTopicId)" />
No
</label>
<label>
<input type="radio" name="IsCovered_#topic.StandardTopicId" />
Yes
</label>
</div>
}
Sample of the output from the above code block:
...
<div id="frame-42" class="topic-frame">
<label>
<input type="radio" name="IsCovered_42" />
No
</label>
<label>
<input type="radio" name="IsCovered_#topic.StandardTopicId" value="No" />
Yes
</label>
</div>
...
Can anyone explain why the last instance of the variable is not being replaced and is instead being rendered as is?
Razor will not parse the "#" in the middle of a word. The underscore is treated as part of a word as opposed to the dash. Use #() to place Razor code within words. #(topic.StandardTopicId)
Here's a reference to Razor syntax: C# Razor Syntax Quick Reference
Most likely, Razor considers IsCovered_#topic.StandardTopicId a valid e-mail address format, and therefore leaves it as-is.

Resources