8

私はこのチュートリアルに従っています: http://www.thymeleaf.org/doc/layouts.html (Thymeleaf Layout Dialect セクションに移動しました)。そこに例があります:

<!DOCTYPE html>
<html>
  <head>
  <!--/*  Each token will be replaced by their respective titles in the resulting page. */-->
    <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">Task List</title>
    ...
  </head>
  <body>
    <!--/* Standard layout can be mixed with Layout Dialect */-->
    <div th:replace="fragments/header :: header">
    ...
    </div>
    <div class="container">
      <div layout:fragment="content">
      ...
      </div>
      <div th:replace="fragments/footer :: footer">&copy; 2014 The Static Templates</div>
    </div>
  </body>
</html>

th:replace上記の例では、フッターとヘッダーはタグに置き換えられていますが、レイアウト ファイルに<head>はタグがあります。<title>

<head>基本的に、タグ全体を に置き換えたいと思いth:replaceます。したがって、私は持っています:

私のレイアウトファイル:

<!DOCTYPE html>
<html>
<head th:replace="/html/components/head :: head">
</head>
<body>
     <div layout:fragment="content">
     </div>
...
     <div th:replace="/html/components/footer :: footer" />
</body>
<html>

私のコンテンツファイル:

<!DOCTYPE html>
<html layout:decorator="/html/layouts/layout">
<head>
    <title>My content title</title>
</head>
<body>
      <div layout:fragment="content">
      ...
      </div>
</body>
</html>

そして最後に /html/components/head.htm ファイル:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head th:fragment="head">
<meta charset="utf-8" />
<title layout:title-pattern="$CONTENT_TITLE">Layout Title should be replaced by Content Title!</title>
...
</head>
<body>
</body>
</html>

内容はまぁまぁ。フッターとヘッダーはファイルから期待どおりに含まれています (置き換えられています) が、ページ タイトルは空白です!

私は得る:

<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title></title>
...

どうしたの?

4

3 に答える 3

32

最後に、私が望んでいたことを達成する方法を見つけました。

レイアウト ファイル内の<title>タグはそのままにしておく必要があります。タグでグループ化し<object>、次のように注釈を付けた他のすべてのタグ:

<head>
  <title layout:title-pattern="$CONTENT_TITLE">Layout Title will be replaced by Page Title!</title>
  <object th:include="/html/components/head :: head" th:remove="tag" />
</head>

私の html/components/head.htm ファイルでは、<title>タグを削除して、インクルード後に重複しないようにする必要がありました。

<head th:fragment="head">
  <meta charset="utf-8" />
  <!-- NO TITLE TAG HERE -->
  ...
</head>

このように head フラグメントがタグに含まれ<object>タグのおかげでth:remove="tag" <object>タグが削除され、最終的な HTML 出力は次のようになります。

<head>
  <title>My content title</title>
  <meta charset="utf-8" />
  <!--  NO TITLE TAG HERE -->
  ...
</head>

明らかに、NO TITLE TAG HERE メッセージも削除しました。

于 2014-10-30T10:10:51.223 に答える