CodeIgniter集成Smarty

简介: CodeIgniter是一款很优秀的轻量级MVC框架,而Smarty是目前最流行的php模板框架。两者配合起来使用,加快开发效率。   第一步:安装CodeIgniter 点击立即下载最新版本的Zip包>> 解压后,复制文件夹下面的application、system、index.php至项目根目录中   第二步:安装Smarty 点击下载最新的Zip包>> 在CodeIgniter的application目录下的third_party目录中新建一个名为smarty的目录,将解压出来的libs包复制到该目录中。

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

目录
相关文章
|
XML API PHP
***CodeIgniter集成微信支付(转)
微信支付Native扫码支付模式二之CodeIgniter集成篇  http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html   CI:3.
1252 0
|
PHP 前端开发
**【ci框架】PHP的CI框架集成Smarty的最佳方式
因为CI自带的模板功能不是很方便,所以大家普遍采用集成Smarty的方式来弥补CI这方面的不足。 本人在网上看了不少CI集成Smarty的教程,包括咱们CI论坛里面的一个精华帖子 http://codeigniter.org.cn/forums/forum.php?mod=viewthread&tid=10345。
1052 0
|
存储 JavaScript 前端开发
CodeIgniter - 集成七牛云存储
最近有一个项目需要集成七牛云存储的图片存储和调用功能,程序是基于CodeIgniter 2.1.3的PHP框架。刚拿到手完全无从下手的感觉,因为像框架这种东西,想从官方的PHPSDK集成进去,需要改动很多地方。
1730 0
|
PHP
zend framework集成smarty
------zf的入口文件-index.php---------
891 0
|
2月前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
161 6
|
2月前
|
Java 关系型数据库 MySQL
如何实现Springboot+camunda+mysql的集成
【7月更文挑战第2天】集成Spring Boot、Camunda和MySQL的简要步骤: 1. 初始化Spring Boot项目,添加Camunda和MySQL驱动依赖。 2. 配置`application.properties`,包括数据库URL、用户名和密码。 3. 设置Camunda引擎属性,指定数据源。 4. 引入流程定义文件(如`.bpmn`)。 5. 创建服务处理流程操作,创建控制器接收请求。 6. Camunda自动在数据库创建表结构。 7. 启动应用,测试流程启动,如通过服务和控制器开始流程实例。 示例代码包括服务类启动流程实例及控制器接口。实际集成需按业务需求调整。
187 4
|
2月前
|
消息中间件 Java 测试技术
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
139 1
|
3月前
|
消息中间件 Java Kafka
springboot集成kafka
springboot集成kafka
120 2
|
3月前
|
监控 前端开发 Java
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
五分钟后,你将学会在SpringBoot项目中如何集成CAT调用链
|
2月前
|
消息中间件 Java Kafka
Spring Boot与Apache Kafka Streams的集成
Spring Boot与Apache Kafka Streams的集成