ci smarty 整合

本文详细介绍了如何将smarty模版引擎整合到CI框架中,以提升PHP项目的开发效率。通过整合,开发者可以享受到更灵活的模板管理和更好的代码组织结构,同时保持了CI框架的原有优势。
目前正在做一个小项目,选择Ci框架也是第一次使用,发现CI没有内置的模版引擎,传统的php、html混写有点不习惯咯被惯坏了,决定将smarty模版引擎整合到CI框架中。
步骤如下:
下载:ci,smarty
配署ci 在这里就不多说了……
1.	将下载好的smarty包的lib文件上传到ci中的libraries 文件中,将取名称修改为smarty,在libraries文件新建cismarty.php文件,内容如下:
if (!defined('BASEPATH')) exit("no direct script access allowd");
//以下是加载smarty的类文件
require_once(APPPATH.'libraries/smarty/Smarty.class.php');
//定义cismarty类,继承smarty类
class cismarty extends Smarty{
	//定义一个受保护的变量,
    protected $ci;

    function __construct(){
        parent::__construct();
		//引用实例化CI,这里主要是将smarty的配置文件写到ci中,以方便程序管理
        $this->ci = & get_instance();
		//加载ci的新建的smarty配置文件
        $this->ci->load->config('smarty');
        $this->cache_lifetime  = $this->ci->config->item('cache_lifetime');
        $this->caching         = $this->ci->config->item('caching');
        $this->template_dir    = $this->ci->config->item('template_dir');
        $this->compile_dir     = $this->ci->config->item('compile_dir');
        $this->cache_dir       = $this->ci->config->item('cache_dir');
        $this->use_sub_dirs    = $this->ci->config->item('use_sub_dirs');
        $this->left_delimiter  = $this->ci->config->item('left_delimiter');
        $this->right_delimiter = $this->ci->config->item('right_delimiter');

2.	在config下新建smarty.php配置文件
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['cache_lifetime']     =     30*24*3600; //更新周期
$config['caching']             =     false;//是否使用缓存,项目在调试期间,不建议启用缓存
$config['template_dir']        =     APPPATH.'views'; //设置模板目录
$config['compile_dir']         =     APPPATH.'views/template_c'; //设置编译目录
$config['cache_dir']         =     APPPATH.'views/cache';//缓存文件夹
$config['use_sub_dirs']     =     true;   //子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter']     =     '<{';
$config['right_delimiter']     =     '}>';


3.	在CI里重载smarty的 assign 和 display方法
在框架根目录下core/目录下新建控制器继承CI基类,MY_Controller
<?php if (!defined('BASEPATH')) exit('No direct access allowed.');
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
    }

    public function assign($key,$val) {
        $this->cismarty->assign($key,$val);
    }

    public function display($html) {
        $this->cismarty->display($html);
    }
}

4.	修改Config文件下的autoload.php 自动加载类文件
$autoload['libraries'] = array('cismarty');
到此配置已完成.
5.	下面测试
a.	新建控制器admin_welcome.php
//if (!define('BASEPATH')) exit('no direct script access allowed');
class Admin_welcome extends MY_Controller{

    function __construct(){
        parent::__construct();
    }
    public function index(){
        //$this->load->view('welcome_message');
        $data['title'] = '标题';
        $data['num'] = '123456789';
        $this->cismarty->assign('data',$data); // 亦可
        $this->cismarty->display('test.html'); // 亦可
        //$this->display('test.html');
    }
}







Views 下新建test.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>smarty配置测试</title>
    </head>
    <body>
    <{$data.title}>
        </body>
        </html>

基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕基于序贯蒙特卡洛模拟法的配电网可靠性评估展开研究,重点介绍了利用Matlab代码实现该方法的技术路径。文中详细阐述了序贯蒙特卡洛模拟的基本原理及其在配电网可靠性分析中的应用,包括系统状态抽样、时序模拟、故障判断与修复过程等核心环节。通过构建典型配电网模型,结合元件故障率、修复时间等参数进行大量仿真,获取系统可靠性指标如停电频率、停电持续时间等,进而评估不同运行条件或规划方案下的配电网可靠性水平。研究还可能涉及对含分布式电源、储能等新型元件的复杂配电网的适应性分析,展示了该方法在现代电力系统评估中的实用性与扩展性。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事电网规划与运行的技术工程师。; 使用场景及目标:①用于教学与科研中理解蒙特卡洛模拟在电力系统可靠性评估中的具体实现;②为实际配电网的可靠性优化设计、设备配置与运维策略制定提供仿真工具支持;③支撑学术论文复现与算法改进研究; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法流程,重点关注状态转移逻辑与时间序列模拟的实现细节,并尝试在IEEE标准测试系统上进行验证与扩展实验,以深化对方法机理的理解。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值