Hope is a Dream. Dream is a Hope.

非公開ブログは再開しました。

いきおいでPHPを勉強する。その12

PEARのHTMLテンプレートを使う

<html>
<head>
<title>PEARのHTMLテンプレートを使う</title>
</head>
<body>

<!-- BEGIN person -->
<li>名前:{name}
<li>点数:{point}
<hr>
<!-- END person -->

</body>
</html>
<?php
// IntegratedTemplate(IT.php)をインクルード
require_once "HTML/Template/IT.php";

// データファイル名
$fname = "tempdata.txt";

// テンプレートクラスのインスタンスを作成する
$it = new HTML_Template_IT("temp");
$it->loadTemplatefile("temp.html", true, true);

// データファイルを開く
$file = fopen($fname, "r");

// 全データを出力する
while (list($name, $point) = fgetcsv($file, 1000, ",")) {

    // 「person」というブロック名の「name」「point」を処理する
    $it->setCurrentBlock("person");
    $it->setVariable("name", $name);
    $it->setVariable("point", $point);
    $it->parseCurrentBlock("person");
}

// ファイルを閉じる
fclose($file);

// テンプレートとデータの結合結果を表示する
$it->show();

?>