is it possible to add extra tag attributes via Thymeleaf 3.0 API during post-processing?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

is it possible to add extra tag attributes via Thymeleaf 3.0 API during post-processing?

Ivan Gammel
Use case is as described below. Suppose I have this template:
<span th:text="#{messageKey}">dummy text</span>
Also, I have L10n resource bundle with value "Localized message" corresponding to key "messageKey". I'd like to get following as output of rendering:
<span data-cms-key="messageKey">Localized message</span>
My goal is to identify all localized strings on the page and allow WYSIWYG editing of them (open source embeddable CMS project).
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to add extra tag attributes via Thymeleaf 3.0 API during post-processing?

danielfernandez
Administrator
Hmmm yes, you can add any attributes during post-processing, but for the scenario you explain I don't think it would be helpful. The reason is, by the time your post-processor executes the "th:text" attribute would no longer exist, so you would have no way of telling whether a tag body text came from an i18n resource bundle or not.

If I were you, I would try to solve this with a mere processor executing on the "th:text" attribute (yes, exactly the same as the "th:text" from the Standard Dialect. The key would be that your processor would have higher precedence than StandardTextTagProcessor so it would execute BEFORE the real "th:text", and your processor would simply check if what is contained inside "th:text" is a #{...} expression and, in such case, create a new "data-cms-key" attribute with its contents. And then NOT delete the "th:text" attribute so that the standard dialect's processor can execute normally...