メインコンテンツまでスキップ
Version: Fegg 5.x

インクルード

include, include head, head, include foot, foot, include html

説明​

{{ include '/path/to/template' }}

別のテンプレートを、同じ変数を使ってこの場所に出力します。インクルードしたテンプレートで設定した変数は、インクルード元には戻りません。

{{ include head '/path/to/template' }}
{{ head }}

{{ include foot '/path/to/template' }}
{{ foot }}

{{ include head }}と{{ include foot }}は、書いた場所には何も出力しません。テンプレートを登録しておき、レイアウトが{{ head }}(<head>の中)や{{ foot }}(</body>の前)を書いた場所で、後から出力します。ページ独自のCSSやJavaScriptを、共通のレイアウトに追加するときに使います。複数のテンプレート(ページと、そのページがtranscludeするレイアウト)が登録した場合は、外側のレイアウトのものが先に出力されます。

{{ include html '/parts/banner.html' }}

{{ include html }}は、ドキュメントルート(htdocs/)の下のファイルを、PHPのincludeで読み込みます。パスは/で始め、拡張子も書きます。テンプレートではないので、中の{{ }}タグはコンパイルされません。

パス​

  • テンプレートのパスには、拡張子.tplを付けません。
  • /で始まるパスは、テンプレートのルートからの相対パスです。テンプレートのルートは、URLが属するサイトのtpl/(/cms/...ではsite/template/cms/tpl/)、次にcore/template/です。こちらを使ってください。どのディレクトリから書いても同じように動きます。
  • /で始まらないパスは、タグを書いたテンプレートのディレクトリからの相対パスです(./parts/card、../include/head/news)。include head/include footでは、登録したテンプレートからの相対パスのままです。
{{ transclude '/template/siteframe' }} site/template/main/tpl/template/siteframe.tpl
{{ transclude '/cms/frame/siteframe' }} site/template/cms/tpl/frame/siteframe.tpl, else core/template/cms/frame/siteframe.tpl
{{ include head '/include/head/about' }} site/template/main/tpl/include/head/about.tpl
{{ include './parts/card' }} parts/card.tpl next to this template
  • 対象が見つからないときは、Fegg\Template\TemplateException(HTTP 500)を投げます。./fegg-cli template:lintを使えば、見つからない対象をデプロイ前に報告できます(Lintとエラーを参照)。

例​

include​

site/template/main/tpl/parts/card.tpl
<div class="c-card">{{ $page.title }}</div>
site/template/main/tpl/news/detail.tpl
<p>Before</p>
{{ include '/parts/card' }}
Output
<p>Before</p>
<div class="c-card">Hello</div>

include head / include foot​

site/template/main/tpl/include/head/about.tpl
<link rel="stylesheet" href="{{ $site.assets }}/css/about.css">
site/template/main/tpl/include/foot/about.tpl
<script src="{{ $site.assets }}/js/about.js"></script>
site/template/main/tpl/about.tpl
{{ transclude '/template/siteframe' }}

{{ include head '/include/head/about' }}
{{ include foot '/include/foot/about' }}

{{ section contents }}
<p>About us</p>
{{ end section contents }}
site/template/main/tpl/template/siteframe.tpl
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>{{ $site.title }}</title>
{{ head }}
</head>
<body>
{{ section contents }}
{{ end section contents }}
{{ foot }}
</body>
</html>
Output
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Example Inc.</title>
<link rel="stylesheet" href="/_fegg/site/template/main/css/about.css">
</head>
<body>
<p>About us</p>
<script src="/_fegg/site/template/main/js/about.js"></script>
</body>
</html>
メモ

{{ include head }}と{{ include foot }}は、{{ section }}の中ではなく、テンプレートの最上位に書きます(Transcludeを参照)。