Application::displayTemplate
渡した変数だけを使ってテンプレートを出力します
説明
public void Application::displayTemplate( string $template, [ array $assignedValue = array() ] )
テンプレートを探し(サイトのtpl/、次にcore/template/)、キャッシュがないか古ければコンパイルして、出力します。使えるのは$assignedValueの変数だけで、$page、$site、$inなどは設定されません。ページを表示するときはApplication::displayPageを使います。
テンプレートエンジン自身も、{{ include }}と{{ transclude }}でこのメソッドを使っています。テンプレートの中では、/で始まらない名前は、そのテンプレートのディレクトリからの相対パスです。コントローラーから呼ぶときは、テンプレートのルートからの相対パスです。
引数
| 名前 | データ型 | デフォルト | 備考 |
|---|---|---|---|
| $template | string | .tplを付けないテンプレート名(foo/bar) | |
| $assignedValue | array | array() | テンプレートに渡す変数。キーtitleは$titleになります |
例
htdocs/_fegg/site/src/Controller/Foo.php
<?php
namespace Site\Controller;
defined('FEGG_CORE_DIR') || exit;
class Foo extends Controller
{
public function bar()
{
$this->displayTemplate('foo/bar', array(
'title' => 'Hello',
'items' => array('a', 'b'),
));
}
}
htdocs/_fegg/site/template/main/tpl/foo/bar.tpl
<h1>{{ $title }}</h1>
{{ foreach $items as $item }}<p>{{ $item }}</p>{{ end foreach }}
{{ if $page.baz }}not shown: $page is not set{{ end }}
Output
<h1>Hello</h1>
<p>a</p><p>b</p>
メモ
テンプレートが見つからないときはFegg\Template\TemplateExceptionを投げます(Lintとエラーを参照)。