Thymeleafのヘッドとタイトル 質問する

Thymeleafのヘッドとタイトル 質問する

私はThymeleaf初心者です。まずは一般的なレイアウトページから始めました。

フラグメント/レイアウト.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>

コンテンツページ:

ページ.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>

ページをレンダリングするとき、タイトルは常にテンプレートから取得され、ページからは取得されません。Thymeleafでは、メタ、スクリプト、スタイルを共通(HEADタグ内)に保持しながら、1ページあたりタイトル?

ベストアンサー1

私も同じ問題を抱えていました(ありがとうんー参照用ドキュメンテーション!) ここで私が気づいたことと、それをアプリでどのように解決したかを示します。

ドキュメントから注目すべき点:

  1. th:includeとの違いth:replace
  2. フラグメントをdomselectorで参照する代わりにth:fragment
  3. thisThymeleafはセレクターを見つけるための「」オプションを提供します

これら 3 つの点を念頭に置いて、次のことを実行できます。

フラグメント/layout.html:

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

ページ.html

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

お役に立てれば!

おすすめ記事