2017-10-28 174 views
0

我有以下片段。我试图用开放的图形标签来扩展基本片段“头部”......但是呈现的页面仅包含片段/头部的标签,以及og ones。在Thymeleaf中扩展片段

我怎么可以添加更多标签片段?

<head th:include="fragments/head :: head"> 
    <!-- You can use Open Graph tags --> 
    <meta property="og:url"   th:content="${url}" /> 
    <meta property="og:type"   content="website" /> 
    <meta property="og:title"   content="GUApp" /> 
    <meta property="og:description" th:content="${description}" /> 
    <!--<meta property="og:image"   content="http://www.your-domain.com/path/image.jpg" />--> 
</head> 

<head th:fragment="head" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
.... 
</head> 

回答

1

最简单的方法是通过额外的代码,如Flexible layouts文档演示。

感谢片段的表达,我们可以为 片段都没有文字,数字,bean对象......但标记,而不是 片段指定参数。

这允许我们创建我们的片段,使得它们可以通过来自调用模板的标记丰富,从而产生非常灵活的模板布局机制。

的index.html

<!DOCTYPE html> 
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> 

    <head th:include="header :: head(~{::meta})"> 
     <!-- You can use Open Graph tags --> 
     <meta property="og:url" th:content="${url}"/> 
     <meta property="og:type" content="website"/> 
     <meta property="og:title" content="GUApp"/> 
     <meta property="og:description" th:content="${description}"/> 
    </head> 

... 

了header.html

<!DOCTYPE html> 
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> 

    <head th:fragment="head(meta)"> 
     <!-- some default styles --> 
     <link href="base.css" rel="stylesheet" /> 

     <!--/* Per-page placeholder for additional meta tags */--> 
     <th:block th:replace="${meta}" /> 
    </head> 

... 

结果HTML:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <link href="base.css" rel="stylesheet" /> 
    <meta property="og:url"/> 
    <meta property="og:type" content="website"/> 
    <meta property="og:title" content="GUApp"/> 
    <meta property="og:description"/> 
</head> 

... 
+0

它应该根据文档的作品,BU t ... 无法解析为片段选择:“fragments/head :: head(〜{:: meta})”(concorso:4) < - 您可以使用Open Graph的标签 - > <元属性= “OG:标题” CONTENT = “GUApp”/> <元属性= “OG:描述” 个:内容= “$ {描述}”/ > <! - - > – Progeny

+0

@Progeny您使用Thymeleaf 2?因为我觉得灵活的布局,仅在第3版。如果不能切换,可以试试[参数化的片段签名(http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#parameterizable-fragment-签名)并路径一些'Map <'property','content'>'而不是标签块。 – varren