Quantcast

How to reuse page title to avoid content duplication?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to reuse page title to avoid content duplication?

Eduardo Leggiero
From:
http://stackoverflow.com/questions/36249720/how-to-reuse-a-title-tag-using-thymeleaf-with-layout

How to change a Layout to reuse the same title text from Template on other tags, avoiding duplicate the text on Template file? See example:

Having this Layout:

  <html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
      <head>
          <meta charset="UTF-8">
          <title>Title from LAYOUT</title>
      </head>
      <body>
          <h1>Title from LAYOUT</title>
          <th:block layout:fragment="content">Default content</th:block>
      </body>
  </html>

And this Template:

  <html xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorator="layout">
      <head>
          <title>Title from TEMPLATE</title>
      </head>
      <body>
          <th:block layout:fragment="content"><p>My content</p></th:block>
      </body>
  </html>

The desired rendered page is:

  <html>
      <head>
          <meta charset="UTF-8">
          <title>Title from TEMPLATE</title>
      </head>
      <body>
          <h1>Title from TEMPLATE</title>
          <p>My content</p>
      </body>
  </html>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to reuse page title to avoid content duplication?

Emanuel
Administrator
From version 1.3.0 of the layout dialect you can actually access the title using a special Thymeleaf variable "layout.resultingTitle".  It was an internal feature to help fix an issue that someone raised, but you can use it for just this purpose.

Just update your layout to read the layout.resultingTitle value in a th:text:

<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
    <head>
        <meta charset="UTF-8">
        <title>Title from LAYOUT</title>
    </head>
    <body>
        <h1 th:text="${layout.resultingTitle}">Template title will go here</title> <!-- Here's the change -->
        <th:block layout:fragment="content">Default content</th:block>
    </body>
</html>

This feature is documented at the end of the section on title configuration: https://github.com/ultraq/thymeleaf-layout-dialect#configuring-your-title
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to reuse page title to avoid content duplication?

Caden Howell
In reply to this post by Eduardo Leggiero
So, granted, it's about a year later, but I have this same question.  I went to the link at  https://github.com/ultraq/thymeleaf-layout-dialect#configuring-your-title, but there is no longer any reference to layout.resultingTitle there.  

What's the solution today?
Loading...