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

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.

主站蜘蛛池模板: 周宁县| 赫章县| 吴堡县| 澳门| 施甸县| 精河县| 临夏市| 孟连| 洛宁县| 嘉黎县| 永宁县| 江口县| 农安县| 宜都市| 罗城| 鲜城| 天气| 九江县| 天柱县| 巴青县| 天台县| 余江县| 宁蒗| 长治市| 民权县| 五莲县| 瑞金市| 洪洞县| 黑山县| 宝清县| 德兴市| 申扎县| 洪湖市| 庆云县| 犍为县| 竹北市| 东丽区| 满城县| 乌海市| 沭阳县| 四会市|