CodeIgniter集成Smarty

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

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

参考网站: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


内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值