MustacheLayout.php 3.21 KB
<?php

/**
 * Created by PhpStorm.
 * User: Hbomb
 * Date: 15-3-12
 * Time: 20:10
 */
namespace Plugin;

use Yaf\Application;
use Yaf\Dispatcher;
use Mustache;

class MustacheLayout extends Layout
{
    protected $config = null;

    public function __construct()
    {
        $this->config = Application::app()->getConfig()->get('application');
        $this->layout = $this->config->layout->default;//默认布局名称是layout
        $this->layout_path = $this->config->layout->path;//系统默认布局路径
    }

    /**
     * 设置视图路径
     */
    public function setScriptPath($path)
    {
        if (is_readable($path)) {
            $this->tpl_dir = $path;
            $this->layout_path = $path . "/../layouts";
            return true;
        }
        throw new \Exception("Invalid path: {$path}");
    }

    /**
     * 获取视图路径
     */
    public function getScriptPath()
    {
        return $this->tpl_dir;
    }


    public function __isset($name)
    {
        return (null !== $this->tpl_vars[$name]);
    }

    public function __unset($name)
    {
        unset($this->tpl_vars[$name]);
    }

    /**
     * 分配视图传参
     */
    public function assign($name, $value = null)
    {
        $this->tpl_vars[$name] = $value;
    }


    public function assignRef($name, &$value)
    {
        $this->tpl_vars[$name] = $value;
    }

    /**
     * 清除视图传参
     */
    public function clearVars($name)
    {
        if (empty($name)) {
            $this->tpl_vars[$name] = array();
        }
        $this->tpl_vars = array();
    }

    /**
     * 渲染视图
     */
    public function render($tpl, $tpl_vars = array())
    {
        $tpl_vars = array_merge($this->tpl_vars, $tpl_vars);
        $dispatcher = Dispatcher::getInstance();
        $module = $dispatcher->getRequest()->module;
        $module_dir = $this->config->directory . '/modules/';//所在模块
        $ext = array('extension' => $this->config->view->ext);//视图扩展名

        if (!$this->tpl_dir)//如果没有设置视图目录的,默认当前模块的目录
        {
            $this->tpl_dir = $module_dir . $module . '/views';
        }

        $viewLoader = new Mustache\Loader\FilesystemLoader($this->tpl_dir, $ext);//设置视图目录
        $partialsLoader = new Mustache\Loader\FilesystemLoader($this->tpl_dir . '/partials', $ext);//设置视图片段目录
        $layoutLoader = new Mustache\Loader\FilesystemLoader($this->layout_path, $ext);//设置布局目录

        //模板引擎初始化
        $m = new Mustache\Engine(array(
            'loader' => $viewLoader,
            'partials_loader' => $partialsLoader,
        ));

        $this->content = $m->render($tpl, $tpl_vars);//视图内容渲染

        if (null == $this->layout) //如果没有布局,直接返回视图内容
        {
            return $this->content;
        }

        $tpl_vars['content'] = $this->content;//设置视图内容

        $m->setLoader($layoutLoader);//设置布局视图的路径
        return $m->render($this->layout, $tpl_vars);//和布局合成渲染html
    }

    /**
     * 显示视图
     */
    public function display($tpl, $tpl_vars = array())
    {
        echo $this->render($tpl, $tpl_vars);
    }
}