我有包含所有页面布局的 template.html 文件,还有另一个 html 文件(index.html),我希望将 index.html 文件的数据写入 template.html 的正文标记中。我怎么能通过百里香做到这一点。
2 回答
            1        
        
		
创建一个页面,其中包含将在页面之间共享的布局。通常这将是一个包含页眉、导航、页脚和页面内容所在位置的模板。
布局.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
   <head>
      <title>Layout page</title>
      <script src="common-script.js"></script>
   </head>
   <body>
      <header>
         <h1>My website</h1>
      </header>
      <section layout:fragment="content">
          <p>Page content goes here</p>
      </section>
      <footer>
         <p>My footer</p>
         <p layout:fragment="custom-footer">Custom footer here</p>
      </footer>  
    </body>
  </html>
注意 layout:fragment 属性是如何应用到 and
页脚中的元素。这些是装饰器页面中的点,可以通过匹配内容页面中的片段进行替换。现在,创建一些内容页面。
内容1.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="Layout.html">
<head>
    <title>Content page 1</title>
    <script src="content-script.js"></script>
</head>
<body>
    <section layout:fragment="content">
     <p>This is a paragraph from content page 1</p>
    </section>
    <footer>
      <p layout:fragment="custom-footer">This is some footer content from content page 1</p>
    </footer>
</body>
</html>
参考:
Thymeleaf 的一种方言,允许您使用布局/装饰器页面来设置您的内容的样式。 https://github.com/ultraq/thymeleaf-layout-dialect#decorators-and-fragments
于 2014-08-27T06:25:16.487   回答
    
    
            0        
        
		
通常你会换一种方式,在你的页面中包含模板或模板的一部分,如下所示:
<head th:include="fragments/template :: headFragment">
在标题中,或
<div th:include="fragments/template :: footerFragment"></div>
在 index.html 的正文中。片段在 template.html 文件中的位置:
<header id="headerFragment" th:fragment="headerFragment">
...
</header>
<div class="container">
</div>
<footer id="footer" th:fragment="footerFragment">
  <!-- FOOTER -->
  <footer>
    ...
  </footer>
</footer>
一旦将索引页面包含到模板中,它就不再是模板了,对吧?
问候,
于 2013-11-29T12:58:45.023   回答