smarty



class mary
{
    public $re;
    public $tmp;
    public $tmp_dir = 'template';
    public $param;

    //渲染模板
    function display($file)
    {
        $this->tmp = $this->tmp_dir . '/' . $file;
        if (!file_exists($this->tmp)||filemtime($this->tmp)<filemtime($file)) {
            $this->bianyi($file);
        }
        $this->inc();
    }

    //1. 给模板变量赋值
    function assign($param)
    {
        $this->param = $param;
    }

    //2. 编译模板
    function bianyi($file)
    {
        $re = @file_get_contents($file);
        $this->check($re);
        $pattern = ['/{\$/', '/}/'];
        $replacement = ['param[\'', '\'];?>'];
        $this->re = preg_replace($pattern, $replacement, $re);
        $this->mk_tmp();
    }

    //3. 写入编译后的模板
    function mk_tmp()
    {

        is_dir($this->tmp_dir) ?: mkdir($this->tmp_dir);
        file_put_contents($this->tmp, $this->re);

    }

    //4. 引入编译后模板
    function inc()
    {
        $re = @include($this->tmp);
        $this->check($re);
    }

    function check($re)
    {
        if (false === $re) {
            $error = error_get_last();
            die($error['message'] . ' in ' . $error['file'] . ' on line ' . $error['line']);
        }
    }

}

你可能感兴趣的:(PHP)