リソースプラグイン

リソースプラグインは、テンプレートソースやPHPスクリプトのコンポーネントを Smarty に提供する一般的な方法と意図されています (例: データベース, LDAP, 共有メモリ, ソケット等)。

各種リソースのために4つの関数を登録する必要があります。 これらの関数の最初のパラメータには要求されたリソースが渡され、 最後のパラメータには Smarty のオブジェクトが渡されます。 残りのパラメータは関数によって異なります。

bool smarty_resource_name_source (string $rsrc_name, string &$source, object &$smarty)

bool smarty_resource_name_timestamp (string $rsrc_name, int &$timestamp, object &$smarty)

bool smarty_resource_name_secure (string $rsrc_name, object &$smarty)

bool smarty_resource_name_trusted (string $rsrc_name, object &$smarty)

例 16-10. リソースプラグイン

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     resource.db.php
 * Type:     resource
 * Name:     db
 * Purpose:  データベースからテンプレートを取得する
 * -------------------------------------------------------------
 */
function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty)
{
    
// ここでデータベースを呼び出し、
    // $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 
smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty)
{
    
// ここでデータベースを呼び出し、$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 
smarty_resource_db_secure($tpl_name, &$smarty)
{
    
// 全てのテンプレートがセキュアであるとみなします
    
return true;
}

function 
smarty_resource_db_trusted($tpl_name, &$smarty)
{
    
// テンプレートでは使用しません
}
?>

register_resource() および unregister_resource() も参照してください。