Hamster
Hamster

Reputation: 680

Why does Thymeleaf code not work in the head section of HTML but works fine in the body

I am using wro4j to minify static content. However, when I am in my development environment I would like to use the uncompressed versions of my JS and CSS files.

To put it into context, I am using Spring Boot and Thymeleaf.

This code does not work inside the <head></head> of my HTML:

<th:block th:if="${@environment.getActiveProfiles()[0] != 'development'}">
    <link th:href="@{/wro4j/compressed.css}" rel="stylesheet"></link>
</th:block>
<th:block th:if="${@environment.getActiveProfiles()[0] == 'development'}">
    <link th:href="@{/css/uncompressed.css}" rel="stylesheet"></link>
</th:block>

What I see from the code above in the source of my HTML is:

<link rel="stylesheet" href="/wro4j/compressed.css" />

<link rel="stylesheet" href="/css/uncompressed.css" /> 

Surely the only css that should be included is uncompressed.css as I have set my profile to be development in 'application.yml'.

However if I were to do something like the following in the <body></body> it would work perfectly as I expect:

<th:block th:if="${@environment.getActiveProfiles()[0] == 'development'}">
    <div th:text="${@environment.getActiveProfiles()[0]}"></div>
</th:block>
<th:block th:if="${@environment.getActiveProfiles()[0] != 'development'}">
    <div th:text="'WHAT THE HECK!' + ${@environment.getActiveProfiles()[0]"></div>
</th:block>

Having set my spring profile to development in application.yml, what I would expect to see from the latter block is "development" and not "WHAT THE HECK! development" and that is exactly what I see. So why is the same code not behaving as I expect in the head section of my HTML.

What am I missing?

Upvotes: 3

Views: 924

Answers (1)

Hamster
Hamster

Reputation: 680

Thanks to @Boris's comment on my question, the solution is to use Thymeleaf version 3. However as a workaround this is what I have done for the time being:

<link th:if="${@environment.getActiveProfiles()[0] != 'development'}"  th:href="@{/wro4j/all.css}" rel="stylesheet" />

<script  th:if="${@environment.getActiveProfiles()[0] == 'development'}" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>

Upvotes: 1

Related Questions