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

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.

主站蜘蛛池模板: 当雄县| 陕西省| 从江县| 永德县| 邓州市| 永宁县| 平阳县| 贡嘎县| 安新县| 南溪县| 丹凤县| 灵武市| 铜鼓县| 湟中县| 芮城县| 兴和县| 左权县| 资中县| 秦皇岛市| 封丘县| 富平县| 香港| 睢宁县| 龙泉市| 长兴县| 中阳县| 香格里拉县| 湾仔区| 都昌县| 历史| 广水市| 佛冈县| 龙岩市| 肥乡县| 玛多县| 益阳市| 繁峙县| 渭南市| 甘南县| 三原县| 海南省|