2015-07-16 12 views
6

मैं एक थाइमेलीफ शुरुआत कर रहा हूं। मैं एक आम लेआउट पेज के साथ शुरू किया:थाइमेलीफ में सिर और शीर्षक

टुकड़े/layout.html

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head th:fragment="headerFragment"> 
    <title>Template title</title> 
    <!-- metas, link and scripts --> 
</head> 
<body> 
    <div class="container"> 
     Some text 
    </div> 
</body> 
</html> 

और सामग्री पृष्ठ:

page.html

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head th:include="fragments/layout :: headerFragment"> 
    <title>Page title</title> 
    <!-- metas, link and scripts --> 
</head> 
<body> 
    <div class="container"> 
     Some text 
    </div> 
</body> 
</html> 

जब मैं पृष्ठ प्रस्तुत करें, शीर्षक हमेशा टेम्पलेट से है, पृष्ठ से नहीं। क्या थाइमेलीफ में मेटा, स्क्रिप्ट और शैली सामान्य (HEAD टैग में) होने के लिए संभव है, लेकिन प्रति पृष्ठ शीर्षक है?

उत्तर

4

आप उन्हें भी जोड़ सकते हैं;) टेम्पलेट पेज में निम्न का उपयोग करें।

<title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE"> 
    Template title 
</title> 

आपके मामले में, इस में हल होगा:

<title>Template title - Page Title</title> 
0

documentation वें के अनुसार: शामिल सहित div में एक टुकड़ा की सामग्री शामिल हैं। तो आपको टेम्पलेट का शीर्षक मिलेगा। आप निम्नानुसार पृष्ठ शीर्षक के लिए एक विशेषता का उपयोग कर सकते हैं और प्रत्येक नियंत्रक में अपना मान निर्धारित कर सकते हैं।

<head th:fragment="headerFragment"> 
    <title>${pagetitle}</title> 
    <!-- metas, link and scripts --> 
</head> 

वैकल्पिक रूप से आप यहां वर्णित अनुसार प्राप्त करने के लिए लेआउट बोलियों का उपयोग कर सकते हैं। Thymeleaf layout dialect and th:replace in head causes title to be blank

10

मैं भी इस समस्या हो रही थी (आप documentation संदर्भित करने के लिए nmy धन्यवाद!) यहाँ मैं क्या देखा है और मैं यह कैसे मेरे एप्लिकेशन में हल:

: प्रलेखन से नोट करने के लिए

हालात

  1. th:include और th:replace
  2. के बजाय domselector द्वारा टुकड़े संदर्भित के बीच मतभेद th:fragment
  3. द्वारा

    टुकड़े/layout.html:

    <head th:fragment="headerFragment"> 
        <title th:include=":: #pageTitle">Layout Generic Title< /title> 
        <!-- metas, link and scripts --> 
    </head> 
    

    पेज

  4. Thymeleaf खोजने चयनकर्ताओं

मन में इन 3 बातों के साथ, आप निम्न कर सकते हैं के लिए एक "this" विकल्प प्रदान करता है। एचटीएमएल

<head th:include="fragments/layout :: headerFragment"> 
    <title id="pageTitle">Page title</title> 
    <!-- other elements you want to reference in your layout --> 
</head> 

उम्मीद है कि इससे मदद मिलती है!

6

Parameterizable fragment signatures देखें।

असल में, अपने टुकड़ा में:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head th:fragment="headerFragment (pageTitle)"> 
    <title th:text="${pageTitle}>Template title</title> 
    <!-- metas, link and scripts --> 
</head> 
<body> 
    <div class="container"> 
     Some text 
    </div> 
</body> 
</html> 

फिर, जहां आप इसे का उपयोग करें:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head th:replace="fragments/layout :: headerFragment (pageTitle='Bla Bla Some Title'"> 
    <title>Page title</title> 
    <!-- metas, link and scripts --> 
</head> 
<body> 
    <div class="container"> 
     Some text 
    </div> 
</body> 
</html> 
+1

धन्यवाद, यह पूरी तरह से काम करता है। – DimaSan

1

मेरे लिए यह काम ..

लेआउट

<head th:fragment="headerfragment"> 
    <title th:text="${pageTitle}">Template Title</title> 

पेज

<head th:include="layout :: headerfragment"> 

मेरी नियंत्रक पर

m.addAttribute("pageTitle", "Dashboard Page"); 
संबंधित मुद्दे