闷崽 发表于 2010-12-23 09:35:17

关于smarty注册函数的问题

最近毕业设计用CI写了个简单的CMS后台,想完善CMS功能,就想弄个跟其他开源CMS那样,有个前台页面的自定义标签。
想到smarty有个register_function ,貌似能实现,但是实施时出现个问题:
先看下controller代码

<?php
class Welcome extends Controller {

        function Welcome()
        {
                parent::Controller();
                $this->load->library("mysmarty");//引入的是文件名,不是类名
        }

        function format_data( $params )
        {
                extract($params);
                echo date($format,$time);
        }
       
        function index()
        {
                $this->mysmarty->assign('time',time());
                $res = $this->mysmarty->register_function('example',array(&$this,'format_data'));
                $this->mysmarty->assign('title', '这是我第一个smarty在CI中的应用!');
                $this->mysmarty->display('index.html');
        }
       
}

HTML页面代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" c>
<title>{$title}</title>
</head>
<body>
{example time="$time" format="Y-m-d"}
{$title}
</body>
</html>


smarty官方手册register_function函数说法:
void register_function (string name, mixed impl, bool cacheable, array or null cache_attrs)
动态注册模板函数插件,前两个参数是模板函数名称和执行函数名称。
执行函数的格式可以是一个包含函数名称的字符串;也可以是一个array(&$object, $method)数组形式,其中&$object是一个对象的引用,而$method是它的一个方法;还可以是一个array(&$ class, $method)数组形式,其中$class是一个类的名称,$method是类中的一个方法。

问题是$res = $this->mysmarty->register_function('example',array(&$this,'format_data'));到底应该怎么写?,PS:我这句代码是不成功的。

我想引用CI里其他类或者对象里的函数,有这方面经验的朋友,能否给我个提示?谢谢!

longjianghu 发表于 2011-1-5 13:26:36

这个东东我也想问,想N久没结果

lazyboy 发表于 2011-1-13 18:36:16

本帖最后由 lazyboy 于 2011-1-13 18:45 编辑

我的建议是
    $this->mysmarty->register_function('example',array($this->get_instance(),'format_data'));
页: [1]
查看完整版本: 关于smarty注册函数的问题