Squareoot
Squareoot

Reputation: 280

Thymeleaf inserts a text in the HTML code

I want to insert an attribute into the HTML Code.
I tried this, but it's not working:

<div id="${var}"> ... </div>

I think you know what I mean. The attribute var should be the ID. I didn't find a solution...

Upvotes: 0

Views: 1846

Answers (3)

Metroids
Metroids

Reputation: 20487

Thymeleaf only evaluates attributes that are prefixed with th:. Here is a list of the attributes that are evaluated:

http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#setting-value-to-specific-attributes

In your case, th:id is already built in so you can simply do <div th:id="${var}"> ... </div> and it will work. th:attr, is used to define attributes that thymeleaf doesn't normally support.

Upvotes: 0

Pau
Pau

Reputation: 16106

You just need to use the th:attr attribute. It is explained in the reference documentation 5.1:

Enter then the th:attr attribute, and its ability to change the value of attributes of the tags it is set in:

<form action="subscribe.html" th:attr="action=@{/subscribe}">  
   <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>   
    </fieldset> 
</form> 

The concept is quite straightforward: th:attr simply takes an expression that assigns a value to an attribute. Having created the corresponding controller and messages files, the result of processing this file will be:

<form action="/gtvg/subscribe">   
   <fieldset>
    <input type="text" name="email" />
    <input type="submit" value="¡Suscríbe!"/>   
  </fieldset> 
 </form>

Upvotes: 3

cralfaro
cralfaro

Reputation: 5948

Use this

<div th:attr="id=${var}"> ... </div>

Upvotes: 0

Related Questions