最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

WeiCot Framework 布局引擎

PHP ajiang-tuzi 5259浏览

朋友想要个布局引擎 我就满足它咯 这个版本比较教老 其实还有个模版引擎 看啥时候有空 拿出来

<?php
/**
 * WeiCot Framework
 * User: ajiang
 * WebSite: WWW.WEICOT<COM
 * Date: 2016/5/29
 * Time: 15:18
 * public  $block;    //Block Class 块
 * static $template = array();  //设置的模板
 * public  templateDir="app/code/";  //模板路径
 * self::$template[] = array(
 * "blockName" => "Base.Header.Function.Value",   //Weicot/Base/Block::Header    function 方法  值 value
 * "templatePath" => "Base/View/Header", // Base/View/Header.phtml
 * );
 */
namespace Weicot\Core;

class View
{
    public $block = "";
    static $template = array();
    static $templateDir = "app/code/";
    static $cssJs = array(
        'css' => '',
        'js' => ''
    );
    static $createBlock = array();

    static $skinDir;
    private static $_init;  //用来控制这个的单例模式

    private function __construct()
    {
        $config = File::getConfig();
        if (isset($config["RootDir"])) {
            $RootDir = isset($config["RootDir"]);
        } else {
            $RootDir = "";
        }
        self::$skinDir = $config["Http"] . "://" . $_SERVER['HTTP_HOST'] . "/" . $RootDir . "skin/frontend/";
    }

    /*
     * 覆盖 clone 方法
    * 禁止克隆
     */

    public function __clone()
    {
        // TODO: Implement __clone() method.
    }

    /*
   * 设置样式
   * return $this
   */
    public function addCss($path, $remote = false)
    {
        if (!$remote) {
            self::$cssJs['css'][] = '<link href="' . self::$skinDir . "css/" . $path . ".css" . '" rel="stylesheet">';
            return $this;
        } else {
            self::$cssJs['css'][] = '<link href="' . $path . '" rel="stylesheet">';
            return $this;
        }
    }

    /*
     * 设置 js
     * returmn$this
     */
    public function addJs($path, $remote = false)
    {
        if (!$remote) {
            self::$cssJs['js'][] = '<script src="' . self::$skinDir . "js/" . $path . '.js"></script>';
            return $this;
        } else {
            self::$cssJs['js'][] = '<script src="' . $path . '"></script>';
            return $this;
        }
    }

    /*
     * 获得模板中添加的
     * css 和 js
     */
    public function getTemplateCssJs()
    {
        if (is_array(self::$cssJs['css'])) {
            foreach (self::$cssJs['css'] as $value) {
                echo $value;
            }
        }
        if (is_array(self::$cssJs['js'])) {
            foreach (self::$cssJs['js'] as $value) {
                echo $value;
            }
        }
    }

    /*
     * 添加模板 目录
     */
    public function setTemplateDir($dir)
    {
        self::$templateDir = $dir;
        return $this;
    }

    /*
     * 设置输出模板
     * 返回数组
     * 将模板信息压入数组中
     * path 和 Block  array
      * return $this
     */
    public function addTemplate($template)
    {

        self::$template[] = $template;
        return $this;

    }


    public function  createBlock($template)
    {
        self:: $createBlock[] = $template;
        return $this;
    }


    public function getJsCss()
    {
        $jsCss = self::$cssJs;
        return $jsCss;
    }

    /*
     * 以压入模板方式设置布局
     * 如果使用默认布局  这加载默认的布局
     * 否则加载默认的布局
     *   "blockName" => "Base.Header",
     *    "templatePath" => "Base/View/Header",
     *    "action"=>"function"
     *     "valu"=>"ccccc"
     * array_unshift(self::$template,...) 压入头部样式
     * 返回 this
     */


    public function setLayout($baseLayout = true)
    {
        if ($baseLayout) {
            array_unshift(self::$template, array(
                "blockName" => "Base.TopMenu",
                "templatePath" => "Base/View/Left",
            ));
            array_unshift(self::$template, array(
                "blockName" => "Base.Page",
                "templatePath" => "Base/View/Head",
            ));
            self::$template[] = array(
                "blockName" => "Base.Page",
                "templatePath" => "Base/View/Footer",
            );
            return $this;
        } else {
            self::$template;
            return $this;
        }

    }

    /*
     * 负责渲染视图
     * 加载并实例化类
     * 渲染方式为 将数组转化为 模板路径
     * 并包含进去
     * Template $this
     * $block=new $blockValue;  实例化块
     * require_once($templatePath); $block-> 并包含模板
     */

    public function toHtml($block = false)
    {
        //在view 视图中创建块
        if (!$block) {
            $template = self::$template;
        } else {
            $template = self:: $createBlock;
        }
        foreach ($template as $value) {
            $blockName = explode('.', $value['blockName']);
            $blockValue = "Weicot\\" . $blockName[0] . "\\Block\\" . $blockName[1];
            $templatePath = BASE_PATH . DS . self::$templateDir . $value['templatePath'] . ".phtml";
            Debug::showPath($blockValue, $templatePath);
            if (file_exists($templatePath)) {
                $block = new $blockValue;
                if (isset($value['action']) && !empty($value['action'])) {
                    //判断是否初始化 block 函数
                    if (isset($value['value']) && !empty($value['value'])) {
                        $block->exe($value['action'], $value['value']);
                    } else {
                        $block->exe($value['action']);
                    }
                }
                require_once($templatePath);
            } else {
                echo "Template Not Found";
            }
        }

    }

    /*
     * 使用 单利模式
     * 让类只实例化一次
     */
    public static function init()
    {
        if (!(self::$_init instanceof self)) {
            self::$_init = new self();
        }
        return self::$_init;
    }


}

转载请注明:(●--●) Hello.My Weicot » WeiCot Framework 布局引擎

蜀ICP备15020253号-1