11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Javascript内でThymeleafを使う方法

Last updated at Posted at 2017-05-16

scriptタグ内で、Tymeleafを使いたいことがあったので共有。

Thymeleaf 3.0から使えます。

(例)こう書きたい


<script>
  <th:block th:if="${!isAlive}">
   if(true){
     console.log('true');
   }
  </th:block>
</script>

しかしエラーになる。

なので、以下のように書く

th:inline="javascript" を記入
[#name ...] or [# ...] に変更

<script th:inline="javascript">
  [#th:block th:if="${!isAlive}"]
   if(true){
     console.log('true');
   }
  [/th:block]
</script>

省略して、こう書くことも可能


<script th:inline="javascript">
  // 省略系
  [# th:if="${!isAlive}"]
   if(true){
     console.log('true');
   }
  [/]
</script>

#参考
https://github.com/thymeleaf/thymeleaf/issues/395
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf_ja.html

11
15
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
11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?