Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: Can I use messages inside expressions

I am using Thymeleaf 3 within a Spring Boot application. Currently I am in a situation where I want to use a message expression inside an EL expression (Spring EL).

First use case: trim the message

data:title="${#{message.key}.trim()}

Second use case: conditionally create an attribute with a message as its value

data:title="${condition ? #{message.key} : ''}

Both examples will produce a syntax error, because #{ is not an allowed start of an expression.

Any ideas how to achieve what I want?

like image 547
obecker Avatar asked Dec 24 '22 11:12

obecker


1 Answers

In both cases you'll want to use the #messages utility object.

data:title="${#messages.msg('key').trim()}"

data:title="${condition ? #messages.msg('key') : ''}"
like image 117
Metroids Avatar answered Dec 28 '22 07:12

Metroids