Sometimes you want to include a specific piece of code to rendered HTML only based on the environment in which the current application is running. A simple example is the Google Analytics tracking code that you only want to be added to the production environment.

Thymeleaf allows us to access the current active Spring application profile to hide or show elements depending on the runtime configuration. In this article, you'll learn how to access the Spring Environment bean in a Thymeleaf template to be used in a conditional statement.

Accessing Environment Bean

In Thymeleaf, you can access all available Spring beans using the @ symbol. Environment is a special Spring bean that represents the current application environment. You can use it to get the currently active profile and then control the elements visibility accordingly.

The Environment bean provides the getActiveProfiles() method that returns an array of strings containing the names of the current active profiles. Profiles in Spring Boot are used to register the beans conditionally, based on the deployment environment.

The following example demonstrates how you can access the Environment bean in Thymeleaf template and display a text only if the current active profile is prod:

<div th:if="${@environment.getActiveProfiles()[0] eq 'prod'}">
    This is the production environment!
</div>

Now if you run the application with the -Dspring.profiles.active=prod command-line argument, you should see the above text displayed on the screen.

Alternatively, you can also specify the current active profile directory in the application.properties or application.yml class by adding the following line:

spring.profiles.active=prod

If more than one profiles are active at a time, a better solution would be to use Thymeleaf's #arrays utility object to check for the presence of the string prod in the active profiles as shown below:

<div th:if="${#arrays.contains(@environment.getActiveProfiles(), 'prod')}">
    This is the production environment!
</div>

Using Environment bean, you can also access other application properties. Let us say you have the following properties defined in your application.properties file:

com.attacomsian.security-salt=3ax5s4Tw!34Sfs4ldd445345
com.attacomsian.failed-login-attempts=4
com.attacomsian.deployment-key=4534VRS

To access the above properties in a Thymeleaf template, you should call the @environment.getProperty() method as shown below:

table>
    <thead>
    <tr>
        <th>Property Name</th>
        <th>Property Value</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>Security Salt</td>
        <td th:text="${@environment.getProperty('com.attacomsian.security-salt')}"></td>
    </tr>
    <tr>
        <td>Failed Login Attempts Allowed</td>
        <td th:text="${@environment.getProperty('com.attacomsian.failed-login-attempts')}"></td>
    </tr>
    <tr>
        <td>Deployment Key</td>
        <td th:text="${@environment.getProperty('com.attacomsian.deployment-key')}"></td>
    </tr>
    </tbody>
</table>

Conclusion

In this article, we looked at how to access the current environment in a Thymeleaf template to conditionally show or hide elements of an HTML document. Spring Boot provides an Environment bean that can be used to access the current active profiles as well as other configuration properties. In Thymeleaf, you can access this bean by using the @ symbol, for example, @envionment.

Read Next: How to use Thymeleaf with Spring Boot

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.