# Thymeleaf switch statement with multiple cases

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Feb 26, 2021. Topics: Thymeleaf.
Canonical URL: https://attacomsian.com/blog/thymeleaf-switch-statement

> A quick guide to learn how to use a switch statement with multiple cases in Thymeleaf.

[Thymeleaf](https://attacomsian.com/blog/spring-boot-thymeleaf-example) provides a way to display content conditionally using the equivalent of a `switch` statement in Java: the `th:switch` and `th:case` attributes set.

The `th:switch` and `th:case` attributes are useful when there are more than two outcomes of an expression. These attributes work not only for constant variables like an enum but also for any other data types such as string or number. 

Here is an example:

```html
<div th:switch="${user.role}">
    <p th:case="'admin'">Administrator</p>
    <p th:case="'manager'">Manager</p>
    <p th:case="'user'">User</p>
</div>
```

Note that as soon as one `th:case` attribute is evaluated as `true`, every other `th:case` attribute in the same switch context is evaluated as `false`. 

`th:case="*"` is used to specify the default option in `th:switch` statement:

```html
<div th:switch="${user.role}">
    <p th:case="'admin'">Administrator</p>
    <p th:case="'manager'">Manager</p>
    <p th:case="'user'">User</p>
    <p th:case="*">Other</p>
</div>
```

Take a look at [this article](https://attacomsian.com/blog/spring-boot-thymeleaf-example) to learn more about how to use Thymeleaf with Spring Boot.
