LoginSignup
3
1

More than 5 years have passed since last update.

Thymeleafのfragmentは、引数の記法を揃えよう

Last updated at Posted at 2015-12-16

Thymeleafをお使いのリーファーの皆様、こんばんは。Thymeleafのfragmentに引数を渡すときにハマったので自分用にメモです。

たとえば以下のような引数を二つ持つフラグメントがあるとしましょう。

<th:block th:fragment="func(flag1, flag2)">
  <p th:if="${flag1 == true}">flag1です。</p>
  <p th:if="${flag2 == true}">flag2です。</p>
</th:block>

それを呼び出すときに、いろいろあって以下のように書いてしまいました。

<th:block th:replace="func(true, flag2=false)" />

非常に残念ながらこれではシステムエラーになります。どうやら記法を揃えなければならなかったようです。次のように書いてあげましょう。

<th:block th:replace="func(flag1=true, flag2=false)" />

もちろん次のようにもかけますが、読みやすさを考えると、上のほうが色々な人に怒られずに済みます。

<th:block th:replace="func(true, false)" />
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1