• CodeIgniter集成Smarty


    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

  • 相关阅读:
    用户故事与敏捷方法阅读笔记2
    用户故事与敏捷方法阅读笔记1
    梦断代码阅读笔记3
    梦断代码阅读笔记2
    梦断代码阅读笔记1
    疫情可视化项目-进度2
    疫情可视化项目-进度1
    每日总结3.16
    Android体温记录器更新-进度5
    每日总结3.11
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/2597191.html
Copyright © 2020-2023  润新知