传统模板引擎的思路是:计算模板变量->设置模板变量(assign)->整合模板并显示页面。
现在想要实现这样的效果:识别模板变量->计算模板变量->整合并显示页面。
这里提供一种思路,利用php的魔术方法 __call 来实现。
<?php
//模板变量处理对象:
class template
{
public function main($op)
{
return $this;
}
public function __call($name,$args)
{
$method_name = 'get_'.$name;
if (!method_exists($this, $method_name))
{
cwarning('找不到模板变量:',$name);
}
if (!empty($args))
{
$r = $this->$method_name($args[0]);
}
else
{
$r = $this->$method_name();
}
return $r;
}
/**
* hello world
* @return string
*/
public function get_test($op)
{
return 'hello world!';
}
}<html> <body> <?php //获得模板变量对象 $o = new template(); $tpl = $o->main(); //声明页面标签 $test = $tpl->test(); ?> <div><?php echo $test; ?></div> </body> </html>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号