Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf read property inside a replace

I have this piece of code, where tdk is a common variable I have defined in the SpringBoot file application.properties named server.contextPath

I would like to know if there is a way to replace it for

<head th:replace="tdk/common/header :: common-header" />

something like

<head th:replace="@environment.get('server.contextPath')/common/header :: common-header" />

I am using

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>1.5.3.RELEASE</version>
</dependency>

I've also tried :

<head th:replace="~{${@environment.getProperty('serverContextPath') + '/common/header'} :: common-header}" />

with this result:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "~{${@environment.getProperty('serverContextPath') + '/common/header'}", template might not exist or might not be accessible by any of the configured Template Resolvers (/tdk/registration/signup:6)
like image 700
Nunyet de Can Calçada Avatar asked May 05 '17 17:05

Nunyet de Can Calçada


People also ask

What is #{} in Thymeleaf?

#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources.

How do you use attributes in Thymeleaf?

In Thymeleaf, these model attributes (or context variables in Thymeleaf jargon) can be accessed with the following syntax: ${attributeName} , where attributeName in our case is messages . This is a Spring EL expression.

Is Thymeleaf better than JSP?

Thymeleaf is a great templating engine which replaces JSP, and you can easily use it in any Spring MVC or Spring Boot application. Unlike JSP it's a pleasure to use.


1 Answers

If you are using thymeleaf 3, you can accomplish this using fragment expressions. I think it should look something like this:

<head th:replace="~{${@environment.getProperty('myPropertyName') + '/common/header'} :: common-header}" />
like image 50
Metroids Avatar answered Dec 03 '22 12:12

Metroids