Thymeleaf: Setting an arbitrary value to an arbitrary attribute
Asked Answered
P

1

6

In my Thymeleaf template I need to set a custom attribute to a dynamically generated value. How would I do that?

I tried th:attr="name=value", but it seems to be pretty much strict about the 'value' part. For instance, I tried to generate the following attribute:

<div ng-init="myUrl='http://myhost.com/something'> ... </div>

where http://myhost.com/something is a dynamic part of the ng-init attrubute and is generated by Thymeleaf's URL expression, like @{...}

Any suggestions how to compose an expression that would produce the above piece of HTML?

Process answered 12/7, 2013 at 17:33 Comment(0)
S
11

Give this a try:

<div th:attr="ng-init='myUrl=\'' + @{http://myhost.com/something} + '\''">...</div>

It will output:

<div ng-init="myUrl=&#39;http://myhost.com/something&#39;">...</div>
Solitary answered 22/8, 2013 at 23:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.