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

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.

主站蜘蛛池模板: 通城县| 江城| 杭锦后旗| 长丰县| 乌鲁木齐市| 息烽县| 焉耆| 卫辉市| 裕民县| 密云县| 彰化县| 赫章县| 神木县| 新兴县| 彭阳县| 山东省| 福州市| 澳门| 牟定县| 闵行区| 苏尼特左旗| 淅川县| 固安县| 乐山市| 嵊泗县| 道真| 抚宁县| 谢通门县| 镇远县| 宁远县| 古交市| 马龙县| 昆山市| 日喀则市| 休宁县| 瑞丽市| 寻乌县| 施甸县| 阿拉善盟| 堆龙德庆县| 临海市|