LoginSignup
5
3

More than 5 years have passed since last update.

ThymeleafでJavascriptを使う方法

Posted at

基本的には、
ThymeleafでJavaScriptのちょっとした注意点をメモにある通り。

個人的にハマった(というか勘違いでちょっと無駄した)箇所を追記。

<noscript> で同じようにThymeleafの値を設定したい場合、最初

<noscript th:inline="javascript">
/*<![CDATA[*/
  <iframe src="http://hogehoge.com/?p=/**[[${param}]]**/param"></iframe>
/*]]>*/
</noscript>

とやっていたけどうまくいかず、よくよく考えたら noscript はただのHTMLなので

<noscript th:inline="javascript">
  <iframe src="http://hogehoge.com/?p=param" th:src="'http://hogehoge.com/?p=' + ${param}"></iframe>
</noscript>

でいけた。

Google Analyticsのタグのように、環境毎に変わるのがキーだけで他が変わらないものを、

<div th:if="${@environment.acceptsProfiles('product')}" th:remove="tag">
  <noscript>
  ...
  </noscript>
  <script>
  ...
  </script>
</div>
<div th:if="${@environment.acceptsProfiles('develop')}" th:remove="tag">
  <noscript>
  ...
  </noscript>
  <script>
  ...
  </script>
</div>

のように繰り返して書くとメンテナンス性に欠けるので、一部だけ置き換えることができるのはかなり便利。

5
3
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
5
3