title: How to do if-else in Thymeleaf?
body: <p>What's the best way to do a simple <code>if</code>-<code>else</code> in Thymeleaf?</p>
  
  <p>I want to achieve in Thymeleaf the same effect as</p>
  
  <pre><code>&lt;c:choose&gt;
    &lt;c:when test="${potentially_complex_expression}"&gt;
       &lt;h2&gt;Hello!&lt;/h2&gt;
    &lt;/c:when&gt;
    &lt;c:otherwise&gt;
       &lt;span class="xxx"&gt;Something else&lt;/span&gt;
    &lt;/c:otherwise&gt;
  &lt;/c:choose&gt;
  </code></pre>
  
  <p>in JSTL.</p>
  
  <p>What I've figured so far:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;div th:with="condition=${potentially_complex_expression}" th:remove="tag"&gt;
      &lt;h2 th:if="${condition}"&gt;Hello!&lt;/h2&gt;
      &lt;span th:unless="${condition}" class="xxx"&gt;Something else&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I don't want to evaluate <code>potentially_complex_expression</code> twice. That's why I introduced local variable <code>condition</code>. Still I don't like using both <code>th:if="${condition}</code> and <code>th:unless="${condition}"</code>.</p>
  
  <p>An important thing is that I use two different HTML tags: let's say <code>h2</code> and <code>span</code>.</p>
  
  <p>Can you suggest a better way to achieve it?</p>