2017-10-28 176 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