CodeIgniter集成Smarty

CodeIgniter集成Smarty
本文介绍如何在CodeIgniter框架中集成Smarty模板引擎,通过六个步骤实现高效开发流程。包括安装配置、示例代码及运行实例。

参考网站:http://www.cnblogs.com/meteoric_cry/archive/2012/07/18/2597191.html


CodeIgniter是一款很优秀的轻量级MVC框架,而Smarty是目前最流行的php模板框架。两者配合起来使用,加快开发效率。

 

第一步:安装CodeIgniter

点击立即下载最新版本的Zip包>>

解压后,复制文件夹下面的application、system、index.php至项目根目录中

 

第二步:安装Smarty

点击下载最新的Zip包>>

在CodeIgniter的application目录下的third_party目录中新建一个名为smarty的目录,将解压出来的libs包复制到该目录中。

 

第三步:创建模板目录

在application目录的views目录中创建两个文件夹templates、templates_c

 

第四步:编写安装代码

我是从http://www.coolphptools.com/codeigniter-smarty 下载的代码,但可能版本问题,并不能直接拿来使用,我修改了部分代码。

Smarty.php(复制至appliction/libraries目录中)

   1: <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
   2:  
   3: /**
   4:  * Smarty Class
   5:  *
   6:  * @package        CodeIgniter
   7:  * @subpackage    Libraries
   8:  * @category    Smarty
   9:  * @author        Kepler Gelotte
  10:  * @link        http://www.coolphptools.com/codeigniter-smarty
  11:  */
  12: //require_once( BASEPATH.'libs/smarty/libs/Smarty.class.php' );
  13: require_once( APPPATH.'third_party/smarty/libs/Smarty.class.php' );
  14:  
  15: class CI_Smarty extends Smarty {
  16:  
  17:     function CI_Smarty()
  18:     {
  19:         parent::Smarty();
  20:  
  21:         $this->compile_dir = APPPATH . "views/templates_c";
  22:         $this->template_dir = APPPATH . "views/templates";
  23:         $this->assign( 'APPPATH', APPPATH );
  24:         $this->assign( 'BASEPATH', BASEPATH );
  25:  
  26:         log_message('debug', "Smarty Class Initialized");
  27:     }
  28:  
  29:     function __construct()
  30:     {
  31:         parent::__construct();
  32:  
  33:         $this->compile_dir = APPPATH . "views/templates_c";
  34:         $this->template_dir = APPPATH . "views/templates";
  35:         $this->assign( 'APPPATH', APPPATH );
  36:         $this->assign( 'BASEPATH', BASEPATH );
  37:  
  38:         // Assign CodeIgniter object by reference to CI
  39:         if ( method_exists( $this, 'assignByRef') )
  40:         {
  41:             $ci =& get_instance();
  42:             $this->assignByRef("ci", $ci);
  43:         }
  44:  
  45:         log_message('debug', "Smarty Class Initialized");
  46:     }
  47:  
  48:  
  49:     /**
  50:      *  Parse a template using the Smarty engine
  51:      *
  52:      * This is a convenience method that combines assign() and
  53:      * display() into one step. 
  54:      *
  55:      * Values to assign are passed in an associative array of
  56:      * name => value pairs.
  57:      *
  58:      * If the output is to be returned as a string to the caller
  59:      * instead of being output, pass true as the third parameter.
  60:      *
  61:      * @access    public
  62:      * @param    string
  63:      * @param    array
  64:      * @param    bool
  65:      * @return    string
  66:      */
  67:     function view($template, $data = array(), $return = FALSE)
  68:     {
  69:         foreach ($data as $key => $val)
  70:         {
  71:             $this->assign($key, $val);
  72:         }
  73:         
  74:         if ($return == FALSE)
  75:         {
  76:             $CI =& get_instance();
  77:             if (method_exists( $CI->output, 'set_output' ))
  78:             {
  79:                 $CI->output->set_output( $this->fetch($template) );
  80:             }
  81:             else
  82:             {
  83:                 $CI->output->final_output = $this->fetch($template);
  84:             }
  85:             return;
  86:         }
  87:         else
  88:         {
  89:             return $this->fetch($template);
  90:         }
  91:     }
  92: }
  93: // END Smarty Class

 

第五步:更新CodeIgniter配置

关于CodeIgniter的配置,豆瓣上有一篇别人写的日记。查看详情>>

这里只是修改application/config/autoload.php文件中的libraries项,让页面自动载入smarty。如果不在这里配置,只需在要用到smarty的地方显示调用$this->load->library(‘smarty’);

 

第六步:运行实例

默认的例子是直接访问你的域名,比如这里meteoric001.com/

或者使用:

meteoric001.com/index.php/welcome/

meteoric001.com/index.php/welcome

meteoric001.com/index.php/welcome/index

 

关于url的设计,可以参考CodeIgniter的用户指南 CodeIgniter URL

 

URL里面带个index.php可能不太好看,这里修改一下服务器配置(nginx为例)

 

最后写一个名叫example的例子,运行效果

application/controllers/example.php

   1: <?php
   2: class Example extends Controller {
   3:  
   4:     function Example()
   5:     {
   6:         parent::Controller();
   7:  
   8:         // $this->load->helper(array('form', 'url'));
   9:         $this->load->library('form_validation');
  10:         $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
  11:     }
  12:  
  13:     function index()
  14:     {
  15:         // This example is taken from the Smarty demo and modified slightly
  16:         $this->smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill");
  17:         $this->smarty->assign("FirstName",array("John","Mary","James","Henry"));
  18:         $this->smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
  19:         $this->smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"), array("I", "J", "K", "L"), array("M", "N", "O", "P")));
  20:  
  21:         $this->smarty->assign("contacts", array(array("phone" => "555-1234", "fax" => "555-2345", "cell" => "999-9999"), array("phone" => "555-4444", "fax" => "555-3333", "cell" => "888-8888")));
  22:  
  23:         $this->smarty->assign("state_values", array( 'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY' ));
  24:         $this->smarty->assign("state_output", array( 'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' ));
  25:  
  26:         // english is the default if you don't set lang
  27:         $this->smarty->assign("lang", "english");
  28:  
  29:         // Set the validation rules if this is a submit
  30:         if ( $this->input->post('action') == 'submit' )
  31:         {
  32:             $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]|xss_clean');
  33:             $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
  34:             $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
  35:             $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
  36:             $this->form_validation->set_rules('state', 'State', '');
  37:  
  38:             if ( ! $this->form_validation->run() )
  39:             {
  40:                 $data['error'] = 'Check and fix the form errors below';
  41:             }
  42:             else
  43:             {
  44:                 $data['message'] = 'Thanks for posting!';
  45:             }
  46:         }
  47:  
  48:         // These assignments are passed by the associative array
  49:         $data['title'] = 'Welcome to the Smarty Website';
  50:         $data['bold'] = true;
  51:         $data['ip_address'] = $this->input->server('REMOTE_ADDR');
  52:  
  53:         // Calling the convenience function view() that allows passing data
  54:         $this->smarty->view( 'example.tpl', $data );
  55:     }
  56: }

 

另外一个页面模板:

 

example的代码可以从这里下载,需要适当做一些修改。立即下载>>

 

本文参考:

CodeIgniter+Smarty - Perfect Together

CodeIgniter URL


【博士论文复现】【阻抗建模、验证扫频法】光伏并网逆变器扫频与稳定性分析(包含锁相环电流环)(Simulink仿真实现)内容概要:本文档是一份关于“光伏并网逆变器扫频与稳定性分析”的Simulink仿真实现资源,重点复现博士论文中的阻抗建模与扫频法验证过程,涵盖锁相环和电流环等关键控制环节。通过构建详细的逆变器模型,采用小信号扰动方法进行频域扫描,获取系统输出阻抗特性,并结合奈奎斯特稳定判据分析并网系统的稳定性,帮助深入理解光伏发电系统在弱电网条件下的动态行为与失稳机理。; 适合人群:具备电力电子、自动控制理论基础,熟悉Simulink仿真环境,从事新能源发电、微电网或电力系统稳定性研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握光伏并网逆变器的阻抗建模方法;②学习基于扫频法的系统稳定性分析流程;③复现高水平学术论文中的关键技术环节,支撑科研项目或学位论文工作;④为实际工程中并网逆变器的稳定性问题提供仿真分析手段。; 阅读建议:建议读者结合相关理论教材与原始论文,逐步运行并调试提供的Simulink模型,重点关注锁相环与电流控制器参数对系统阻抗特性的影响,通过改变电网强度等条件观察系统稳定性变化,深化对阻抗分析法的理解与应用能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值