codecamp

PHP原生模板

使用原生PHP作为模板语言

这儿的演示只是我个人的写法,大家有别的方法可以随意

我们可以在公共(模块)控制器中定义一个存放模板目录路径的常量或是变量,比如:

<?php
/**
 * 前台公共控制器,继承控制器基类
 */
namespace app\home\controller;


class Common
{
    //模板目录
    public $tpl_dir;

    public function __construct()
    {
        $this->tpl_dir = APP_PATH . 'home/view/default/';

    }

}

接着在子控制器中继承公共控制器,直接需要调用模板文件的地方include就行

<?php
/**
 * 首页控制器,继承公共控制器类
 */
namespace app\home\controller;


class Index extends Common
{
    public function index()
    {
        //定义一些常量
        include $this->tpl_dir . 'index/index.php';
    }
}
公共控制器
Smarty模板引擎
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

控制器

附录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }