Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

変数のスコープ

変数を代入する際には、メイン Smarty オブジェクト、 createData() で作ったデータオブジェクト、 createTemplate() で作ったテンプレートオブジェクト のいずれかのスコープを選ぶことができます。 これらのオブジェクトは連結することができます。 テンプレートからは、自分自身のオブジェクトの変数だけでなく 連結した親オブジェクトに代入された変数も見ることができます。

デフォルトでは、$smarty->display(...)$smarty->fetch(...) でレンダリングしたオブジェクトは自動的に Smarty オブジェクトの変数スコープにリンクされます。

個々のデータあるいはテンプレートのスコープに代入すれば、 テンプレートからどの変数が見えるのかを完全に制御することができます。

Example 4.6. 変数のスコープの例



// 変数を Smarty オブジェクトのスコープに代入します
$smarty->assign('foo','smarty');

// 変数をデータオブジェクトのスコープに代入します
$data = $smarty->createData();
$data->assign('foo','data');
$data->assign('bar','bar-data');

// 変数を他のデータオブジェクトのスコープに代入します
$data2 = $smarty->createData($data);
$data2->assign('bar','bar-data2');

// 変数をテンプレートオブジェクトのスコープに代入します
$tpl = $smarty->createTemplate('index.tpl');
$tpl->assign('bar','bar-template');

// 変数を Smarty オブジェクトへのリンクを持つテンプレートオブジェクトのスコープに代入します
$tpl2 = $smarty->createTemplate('index.tpl',$smarty);
$tpl2->assign('bar','bar-template2');

// この display() は、$smarty オブジェクトの $foo='smarty' が見えます
$smarty->display('index.tpl');

// この display() は、データオブジェクト $data の $foo='data' と $bar='bar-data' が見えます
$smarty->display('index.tpl',$data);

// この display() は、データオブジェクト $data の $foo='data' と
// データオブジェクト $data2 の $bar='bar-data2' が見えます
$smarty->display('index.tpl',$data2);

// この display() は、テンプレートオブジェクト $tpl の $bar='bar-template' が見えます
$tpl->display();  // あるいは $smarty->display($tpl);

// この display() は、テンプレートオブジェクト $tpl2 の $bar='bar-template2' と
// Smarty オブジェクト $foo の $foo='smarty' が見えます
$tpl2->display();  // あるいは $smarty->display($tpl2);

    


assign()createData() および createTemplate() も参照ください。

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors