Thinkphp Smarty 完美搭建WebApplication

本文介绍如何将Smarty模板引擎集成到ThinkPHP框架中,并提供详细的配置步骤及类文件实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php

namespace Libs\Vendor\Smarty;

/**
 * Libs在APP_PATH目录下
 * Class TplSmarty
 * @package Libs\Vendor\Smarty
 * 修改Think/ParseTemplateBehavior.class.php
 *  $engine             =   strtolower(C('TMPL_ENGINE_TYPE'));
 *  修改为
 *  $engine = C('TMPL_ENGINE_TYPE');        // 使用上面的错误提示系统不支持
 * 配置文件中添加Smarty的配置项
 * // 模板配置
 * 'TMPL_ENGINE_TYPE'  =>  'Libs\\Vendor\\Smarty\\TplSmarty',   //Smarty模板引擎
 */

class TplSmarty{

    private  $smartyTpl = null;

    public function __construct(){
        vendor('Smarty.Smarty#class');
        if(empty($this->smartyTpl) || !($this->smartyTpl instanceof TplSmarty) ){
            $smarty = new \Smarty();
            $smarty->caching = C('TMPL_CACHE_ON');
            $smarty->template_dir = THEME_PATH;
            $smarty->compile_dir = CACHE_PATH;
            $smarty->cache_dir = TEMP_PATH;
            if(C('TMPL_ENGINE_CONFIG')) {
                $config  =  C('TMPL_ENGINE_CONFIG');
                foreach ($config as $key=>$val){
                    $smarty->{$key}   =  $val;
                }
            }
            $this->smartyTpl = $smarty;
        }
    }

    /**
     * 渲染模板输出
     * @access public
     * @param string $templateFile 模板文件名
     * @param array $var 模板变量
     * @return void
     */
    public function fetch($templateFile,$var) {
        $this->smartyTpl->assign($var);
        $this->smartyTpl->display($templateFile);
    }

} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值