template_exists() (Smarty クラスメソッド) - PHPプロ!マニュアル

Smartyマニュアル

Smarty クラスメソッド - template_exists()

template_exists()

template_exists() -- 指定したテンプレートが存在するかどうかをチェックします。

説明

bool template_exists ( string template)

ファイルシステムに関するテンプレートへのパス 又はテンプレートを指定するリソースの文字列のいずれかを受け入れる事ができます。

例 13-1. template_exists()

この例は、コンテンツテンプレートを インクルード するのに $_GET['page'] を使用しています。 テンプレートが存在しない場合、代わりにエラーページが表示されます。 まずは page_container.tpl から。

<html>
<head><title>{$title}</title></head>
<body>
{include file='page_top.tpl'}

{* コンテンツページの中央部分をインクルード *}
{include file=$content_template}

{include file='page_footer.tpl'}
</body>

そしてスクリプトです。

<?php

// index.inc.tpl のようにファイル名をセットします
$mid_template $_GET['page'].'.inc.tpl';

if( !
$smarty->template_exists($mid_template) ){
    
$mid_template 'page_not_found.tpl';
}
$smarty->assign('content_template'$mid_template);

$smarty->display('page_container.tpl');

?>

display()fetch(){include} および {insert} も参照してください。



Pick Up Q&A

Q
array_mergeの再帰処理の動作について
 このエントリーをはてなブックマークに追加 
A
>1個になったとき$leftを返しますが、 >このとき、最終的な$leftはnullになるかと思います。 いいえ、最後は「渡された配列をそのまま」返します。要素が2以上あるときとの違いは(並べ替えずに戻るので...

>>続きを読む

再帰関数は最初の内は混乱しますが、非常に上手く使える場面もいずれ出てきます。これを機会に学んでいけるといいですね。

▲解説者:岡本(アシアル株式会社 教育コーディネーター兼 システムエンジニア)