Advanced Features

PHP Smarty

Smarty - the compiling PHP template engine
Prev 来源:PHP中文社区 Next

Chapter 15. Advanced Features

高级特点

Table of Contents
Objects 对象
Prefilters 预过滤器
Postfilters 后过滤器
Output Filters 输出过滤器
Cache Handler Function 缓冲处理函数
Resources 资源

Objects

对象

<?php
// the object

class My_Object() {
	function meth1($params, &$smarty_obj) {
		return "this is my meth1";
	}
}

$myobj = new My_Object;
// registering the object (will be by reference)
$smarty->register_object("foobar",$myobj);
// if we want to restrict access to certain methods or properties, list them
$smarty->register_object("foobar",$myobj,array('meth1','meth2','prop1'));
// if you want to use the traditional object parameter format, pass a boolean of false
$smarty->register_object("foobar",$myobj,null,false);

// We can also assign objects. Assign by ref when possible.
$smarty->assign_by_ref("myobj", $myobj);

$smarty->display("index.tpl");
?>

TEMPLATE:

{* access our registered object *}
{foobar->meth1 p1="foo" p2=$bar}

{* you can also assign the output *}
{foobar->meth1 p1="foo" p2=$bar assign="output"}
the output was {$output)

{* access our assigned object *}
{$myobj->meth1("foo",$bar)}

Prev 来源:PHP中文社区 Home Next
Cache Groups Up Prefilters