JayC
JayC

Reputation: 2292

How to do a th:if statement in thymeleaf?

How can I do a th:if statement to match 2 expressions?

 <form class="form-horizontal" th:object="${server}" th:action="@{/addServer}" method="POST">
     <input type="hidden" th:field="*{id}"/>

    <div class="form-group">
     <label class="col-sm-2 control-label">Host:</label>
      <div class="col-sm-10">
       <input type="text" style="width: 500px" class="form-control" th:field="*{host}"/>
</div>
</div>
</form>

<!----------------------------------------------------------------------------->
<tr th:object="${test}">
    <td th:text ="${test.Status}"></td>
     <td th:text="${test.host}"></td>
    <td th:text="${test.version}"></td>
</tr>

How can I check if test.host is = to th:field="*host"? Is this even possible?

Upvotes: 3

Views: 17216

Answers (1)

Metroids
Metroids

Reputation: 20477

*{host} is the same as ${server.host} (because server is your form's th:object). To compare it with something else you can use that same expression. Something like:

th:if="${server.host == test.host}"

EDIT: does this do what you want?

<table>
  <tr th:each="t: ${test}" th:if="${server.host == t.host}">
    <td th:text="${t.Status}" />
    <td th:text="${t.host}" />
    <td th:text="${t.version}" />
  </tr>
</table>

Upvotes: 2

Related Questions