SEARCH

このマニュアルはSmartyのものです。セキュリティなどの問題でRCMSでは利用できないものもありますので、ご注意ください。
テンプレートリソース

テンプレートリソース

テンプレートは様々なリソースから呼び出して使用できます。テンプレートを display()fetch() したり別のテンプレートからインクルードしたりする際には、 リソースの種類に続けて適切なパスとテンプレート名を指定します。 リソースを明示的に指定しない場合は $default_resource_type の値であるとみなします。

$template_dir からのテンプレート

$template_dir からのテンプレートを使用する場合は、 テンプレートリソースの指定は必要ありません。しかし、一貫性を保つために file: リソースを使用してもかまいません。使用したいテンプレートへのパスを、 $template_dir のルートディレクトリからの相対パスで指定します。

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

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

Smarty のテンプレート

{include file='index.tpl'}
{* 以下は、上と同じです *}
{include file='file:index.tpl'}

任意のディレクトリからのテンプレート

$template_dir の外に置かれたテンプレートを使うには、リソースの種類 file: を指定しなければなりません。 その後にテンプレートへの絶対パスを続けます。

例 15-7. 任意のディレクトリからのテンプレートを使用する

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

Smarty のテンプレート

{include file='file:/usr/local/share/templates/navigation.tpl'}

Windows のファイルパス

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

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

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

Smarty テンプレート

{include file='file:D:/usr/local/share/templates/navigation.tpl'}

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

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

リソースプラグイン関数についての詳細な情報は リソースプラグイン の項を参照してください。

注意: 元から存在する file: リソースは上書きできないことに注意しましょう。 しかし、ファイルシステム上のテンプレートを別の方法で取得するテンプレートを作成することはできます。 それを別のリソース名で登録すればよいのです。

例 15-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");
?>

Smarty テンプレート

{include file='db:/extras/navigation.tpl'}

デフォルトのテンプレートハンドラ関数

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

例 15-10. デフォルトのテンプレートハンドラ関数を使用する

<?php
// アプリケーション内のどこかでこの関数を呼び出します

function make_template ($resource_type$resource_name, &$template_source, &$template_timestamp,
&
$smarty_obj)
{
    if( 
$resource_type == 'file' ) {
        if ( ! 
is_readable $resource_name )) {
            
// テンプレートファイルを生成し、コンテンツを返します
            
$template_source "This is a new template.";
            require_once 
SMARTY_CORE_DIR 'core.write_file.php';
            
smarty_core_write_file( array( 'filename'=>$smarty_obj->template_dir DIRECTORY_SEPARATOR $resource_name'contents'=>$template_source ), $smarty_obj ); 
            return 
true;
        }
    } else {
        
// ファイルではない場合
        
return false;
    }
}

// デフォルトのハンドラをセット
$smarty->default_template_handler_func 'make_template';
?>

SEARCH