wecenter学习笔记-模版视图渲染框架Savant3

字数 64阅读 413

该文是wecenter学习笔记的一部分

核心代码如下:

public function fetch($tpl = null)
    {
        // make sure we have a template source to work with
        if (is_null($tpl)) {
            $tpl = $this->__config['template'];
        }
        
        // get a path to the compiled template script
        $result = $this->template($tpl);
        
        // did we get a path?
        if (! $result || $this->isError($result)) {
        
            // no. return the error result.
            return $result;
            
        } else {
        
            // yes.  execute the template script.  move the script-path
            // out of the local scope, then clean up the local scope to
            // avoid variable name conflicts.
            $this->__config['fetch'] = $result;
            unset($result);
            unset($tpl);
            
            // are we doing extraction?
            if ($this->__config['extract']) {
                // pull variables into the local scope.
                extract(get_object_vars($this), EXTR_REFS);
            }
            
            // buffer output so we can return it instead of displaying.
            ob_start();
            
            // are we using filters?
            if ($this->__config['filters']) {
                // use a second buffer to apply filters. we used to set
                // the ob_start() filter callback, but that would
                // silence errors in the filters. Hendy Irawan provided
                // the next three lines as a "verbose" fix.
                ob_start();
                include $this->__config['fetch'];
                echo $this->applyFilters(ob_get_clean());
            } else {
                // no filters being used.
                include $this->__config['fetch'];
            }
            
            // reset the fetch script value, get the buffer, and return.
            $this->__config['fetch'] = null;
            return ob_get_clean();
        }
    }

分两步:

  1. 将assign设置到Savant3实例的成员变量导入到当前符号表中

    extract(get_object_vars($this), EXTR_REFS);

  2. 执行模版
    include $this->__config['fetch'];


o→Zend Session 框架

推荐阅读更多精彩内容