Search code examples
thymeleaftemplate-engine

Thymeleaf: combine #strings functions


Is it possible to do something like this th:attr="some-data=${#strings.replace(#strings.toLowerCase(object), '\\s', '-')} - if so, is there a shorter way to do this? Thanks.


Solution

  • That type of text manipulation is possible ... without the regex-expression (I think the problem with regex here is that thymleaf escapes the expression):

    "${#strings.replace(#strings.toLowerCase(object), ' ', '-')}"
    

    if you want to set a custom-attribute 'some-data' you can use:

    th:attr="some-data=${#strings.replace(#strings.toLowerCase(object), ' ', '-')}"
    

    or a little bit shorter for newer versions of tymeleaf:

    th:some-data="${#strings.replace(#strings.toLowerCase(object), ' ', '-')}