官术网_书友最值得收藏!

Defining the view templates

We will be using the Thymeleaf template engine for handling server-side templates. Thymeleaf provides various dialects and conditional blocks for rendering the dynamic content within the static HTML. Let's look at some simple syntactical element of Thymeleaf, as follows:

<!-- Dynamic content in HTML tag -->
<div class="alert alert-info">[[${country.name}]]</div>

<!-- Dynamic attributes -->
<span th:class="|alert ${error ? 'alert-danger': _}|">[[${errorMsg}]]</span>

<!-- Looping -->
<ol>
<li th:each="c : ${countries}">
[[${c.name}]]
</li>
</ol>

<!-- Conditionals -->
<div class="alert alert-warning" th:if="${count == 0}">No results found</div>

<!-- Custom attributes -->
<div th:attr="data-count=${count}"></div>

<!-- Form element value -->
<input type="text" th:value="${country.name}" name="name" />

From the previous examples, we can observe that the items to be evaluated by Thymeleaf are prefixed with th: and any content to be rendered between the tags can be done either using th:text or [[${variable}]]. The latter syntax has been introduced in Thymeleaf 3. This was a very short primer, as going in to depth on Thymeleaf is out of the scope of this book. A beautiful guide explaining different parts of the template can be found at http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html.

主站蜘蛛池模板: 余庆县| 峨边| 绥滨县| 新余市| 张家川| 大安市| 岳阳市| 嘉鱼县| 小金县| 淳化县| 沾益县| 乌海市| 瑞安市| 商河县| 石狮市| 永定县| 尼木县| 沧源| 环江| 文山县| 蕉岭县| 望江县| 英吉沙县| 台北市| 大渡口区| 垫江县| 鄂托克旗| 堆龙德庆县| 延川县| 周宁县| 波密县| 稷山县| 通辽市| 丽水市| 射洪县| 甘洛县| 新河县| 东方市| 鄂托克前旗| 雷波县| 宜宾市|