smarty @Wiki

テンプレートリソース

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集

テンプレートリソース

テンプレートは様々なリソースから呼び出して使用できます。テンプレートをdisplay, fetch, includeする際には適切なパスとテンプレートファイル名とリソースの種類を指定します。リソースが明示的に与えられない時は $default_resource_type の値であると仮定されます。

$template_dir からのテンプレート

$template_dirのテンプレートを使用する場合はテンプレートリソースの指定は必要ありません。$template_dirからの相対パスによってテンプレートファイルの場所を指定して下さい。

例 14-6. $template_dirのテンプレートを使用する

<?php
$smarty->display("index.tpl");
$smarty->display("admin/menu.tpl");
$smarty->display("file:admin/menu.tpl"); // 上と同じ
?>

{* テンプレートファイル内 *}
{include file="index.tpl"}
{include file="file:index.tpl"} {* 上と同じ *}


色々なディレクトリからのテンプレート

$template_dirの外に置かれたテンプレートを使うには、テンプレートの絶対パスとファイル名に加えてリソースの種類"file:"の指定が必要です。

例 14-7. 色々なディレクトリからのテンプレートを使用する

<?php
$smarty->display("file:/export/templates/index.tpl");
$smarty->display("file:/path/to/my/templates/menu.tpl");
?>

{* テンプレートファイル内 *}
{include file="file:/usr/local/share/templates/navigation.tpl"}


Windowsファイルパス

通常、Windows環境の場合はファイルパスにドライブレター(C:)が含まれます。ネームスペースの衝突を回避するために、必ず"file:"を使用して下さい。

例 14-8. Windowsファイルパスからテンプレートを使用する

<?php
$smarty->display("file:C:/export/templates/index.tpl");
$smarty->display("file:F:/path/to/my/templates/menu.tpl");
?>

{* from within Smarty template *}
{include file="file:D:/usr/local/share/templates/navigation.tpl"}


その他のリソース内のテンプレート

データベース・ソケット・LDAP等のphpによってアクセス可能なリソースからテンプレートを取得する事ができます。そのためにはリソースプラグイン関数を記述し、それを登録する必要があります。

リソースプラグイン関数については、リソースプラグインの項を参照して下さい。

注意: 元から存在するfileリソースをオーバーライドする事はできませんが、それとは別のリソース名で登録することによってファイルシステムから異なる方法でテンプレートを取得するためのテンプレートリソースを提供できる事に注意して下さい。

例 14-9. 定義したリソースを使用する

<?php
 // これらの関数をアプリケーションに追加する
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
   // ここでデータベースを呼び出し、取得したテンプレートを
   // $tpl_source に代入する
   $sql = new SQL;
   $sql->query("select tpl_source
                  from my_table
                 where tpl_name='$tpl_name'");
   if ($sql->num_rows) {
       $tpl_source = $sql->record['tpl_source'];
       return true;
   } else {
       return false;
   }
}

function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
   // $tpl_timestampに代入するためにデータベースを呼び出す
   $sql = new SQL;
   $sql->query("select tpl_timestamp
                  from my_table
                 where tpl_name='$tpl_name'");
   if ($sql->num_rows) {
       $tpl_timestamp = $sql->record['tpl_timestamp'];
       return true;
   } else {
       return false;
   }
}

function db_get_secure($tpl_name, &$smarty_obj)
{
   // 全てのテンプレートがセキュアであると仮定する
   return true;
}

function db_get_trusted($tpl_name, &$smarty_obj)
{
   // テンプレートから使用しない
}

 // テンプレートリソース名"db"を登録する
$smarty->register_resource("db", array("db_get_template",
                                      "db_get_timestamp",
                                      "db_get_secure",
                                      "db_get_trusted"));

 // phpスクリプトからテンプレートリソースを使用する
$smarty->display("db:index.tpl");
?>

{* テンプレートからテンプレートリソースを使用する *}
{include file="db:/extras/navigation.tpl"}


テンプレートハンドラ関数

テンプレートリソースからテンプレートの取得に失敗した際に、テンプレートのコンテンツを取り戻すために呼び出されるユーザ定義関数を指定します。この関数の使用方法の1つとして、その場限りのテンプレートを作成する処理を行います。

例 14-10. using the default template handler function

<?php
 // put this function somewhere in your application

function make_template ($resource_type, $resource_name, &$template_source, &$template_timestamp, &$smarty_obj)
{
   if( $resource_type == 'file' ) {
       if ( ! is_readable ( $resource_name )) {
           // create the template file, return contents.
           $template_source = "This is a new template.";
           $template_timestamp = time();
           $smarty_obj->_write_file($resource_name,$template_source);
           return true;
       }
   } else {
       // not a file
       return false;
   }
}

 // set the default handler
$smarty->default_template_handler_func = 'make_template';
?>
ウィキ募集バナー