テンプレートファイル: authors.tpl
<html> <head><title>{PAGE_TITLE}</title></head> <body> <table> <caption>作者</caption> <thead> <tr><th>名前</th><th>Email</th></tr> </thead> <tfoot> <tr><td colspan="2">{NUM_AUTHORS}</td></tr> </tfoot> <tbody> <!-- BEGIN authorline --> <tr><td>{AUTHOR_NAME}</td><td>{AUTHOR_EMAIL}</td></tr> <!-- END authorline --> </tbody> </table> </body> </html>
PHP のコード: authors.php
<?php
// この作者一覧を表示します
$authors = array(
'Christian Weiske' => '[email protected]',
'Bjoern Schotte' => '[email protected]'
);
require_once 'HTML/Template/PHPLIB.php';
// テンプレートオブジェクトを作成します
$t =& new HTML_Template_PHPLIB(dirname(__FILE__), 'keep');
// ファイルを読み込みます
$t->setFile('authors', 'authors.tpl');
// ブロックを設定します
$t->setBlock('authors', 'authorline', 'authorline_ref');
// 変数の値を設定します
$t->setVar('NUM_AUTHORS', count($authors));
$t->setVar('PAGE_TITLE', 'Code authors as of ' . date('Y-m-d'));
// 作者を表示します
foreach ($authors as $name => $email) {
$t->setVar('AUTHOR_NAME', $name);
$t->setVar('AUTHOR_EMAIL', $email);
$t->parse('authorline_ref', 'authorline', true);
}
// 終了処理を行い、出力します
echo $t->finish($t->parse('OUT', 'authors'));
?>