thymeleaf之fragment

MUEAS项目,web前端采用thymeleaf作为展示层。这个view解析器,个人觉得非常不错。简单而且性能也比较好!个人觉得比JSP和freemarker之类,简单易用!

今天简单记录一下fragment的使用,这个类是JSP的tag,但是确非常简单。直接在html文件中,将自己觉得可能在多个地方出现的元素块用fragment包起来!

例如:

 <!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div th:fragment="footer">
&copy; 2013 Footer
</div>
</body>
</html>

在使用的地方,再用include标签将其引入即可!

例如:

 <!DOCTYPE html>
<html>
<head>
<!--/* Each token will be replaced by their respective titles in the resulting page. */-->
<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
...
</head>
<body>
<!--/* Standard layout can be mixed with Layout Dialect */-->
<div th:replace="fragments/header :: header">
...
</div>
<div class="container">
<div layout:fragment="content">
<!-- ============================================================================ -->
<!-- This content is only used for static prototyping purposes (natural templates)-->
<!-- and is therefore entirely optional, as this markup fragment will be included -->
<!-- from "fragments/header.html" at runtime. -->
<!-- ============================================================================ -->
<h1>Static content for prototyping purposes only</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent scelerisque neque neque, ac elementum quam dignissim interdum.
Phasellus et placerat elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent scelerisque neque neque, ac elementum quam dignissim interdum.
Phasellus et placerat elit.
</p>
</div>
<div th:replace="fragments/footer :: footer">&copy; 2014 The Static Templates</div>
</div>
</body>
</html>

注意:th:fragment定义的片段,在需要的地方,可以用th:include或者th:replace进行带入!

还有,所有的fragment可以写在一个文件里面,也可以单独存在。可以是一个html文件中的一部分。只要你需要,带上th:fragment的标签进行定义,让其为一个fragment即可。有点需要注意的是,片段所在的文件的路径,要用“/”分开路径,根路径为templates所在的路径。

比如:

 <body>
<div class="container">
<div th:include="exam/special/geeker/geeker::geeker-base-header"></div>
<div class="geeker-content">
<div th:include="exam/special/geeker/geeker::geeker-base-left"></div>
<div class="geeker-main">
</div>
<div class="geeker-right">
</div>
</div>
</div>
</body>

是不是很简单易用?我觉得很不错!

上一篇:python中__init__()、__new__()、__call__()、__del__()用法


下一篇:iOS方法封装