Nov 5

逆引きrhaco(?)12:テンプレートを使ってHTMLを出力する

テンプレートを使ってHTMLを出力するにはHtmlParserやFlowを使う方法があります。

Rhaco::import('tag.HtmlParser');
$parser = new HtmlParser();
$parser->setVariable('foo','bar');
$parser->write('path/To/template');

テンプレートは通常setup.phpのあるフォルダ/resources/templates以下に用意します。例えば上の例ではresources/templates/path/To/templateに
<html>
 <head>
  <title>テスト</title>
 </head>
 <body>
  {$foo}
 </body>
</html>

というテンプレートを用意すると
<html>
 <head>
  <title>テスト</title>
 </head>
 <body>
  bar
 </body>
</html>

と出力されます。
Flowを用いる場合は
Rhaco::import('generic.Flow');
$flow = new Flow();
$flow->setVariable('foo','bar');
$flow->write('path/To/template');

で同じことが出来ます。Flowはpost,getやBasic認証を扱うことが出来るので非常に便利です。

| comment(0)

このエントリーのはてなブックマーク (-)