• CodeIgniter中使用Smarty模板引擎


    在CodeIgniter框架中,parser解析方式不是很完善,所以想换用smarty模版引擎。

    方法如下:

    1、下载smarty源码包,解压后将其中的lib文件夹复制到CI框架中的application/library/下面;

    2、同时在该目录下建立一个Cismarty.php文件,文件内容如下:

    View Code
     1 <?php 
     2  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
     3 require_once( APPPATH . 'libraries/Smarty/Smarty.class.php' );
     4 /**
     5  * Smarty Class
     6  *
     7  * Lets you use smarty engine in CodeIgniter.
     8  *
     9  * @package        Application
    10  * @subpackage    Libraries
    11  * @category    Customize Class
    12  * @author        sharexie
    13  */
    14 class Cismarty extends Smarty {
    15 
    16     var $CI;
    17 
    18     /**
    19      * Smarty constructor
    20      *
    21      * The constructor runs the session routines automatically
    22      * whenever the class is instantiated.
    23      */
    24     public function __construct()
    25     {
    26         parent::__construct();
    27 
    28         $this->CI =& get_instance();
    29         $this->template_dir = APPPATH . 'views/';
    30         $this->compile_dir = 'cache/templates_c/';
    31         //$this->config_dir = $CI->config->item('configs_dir');
    32         //$this->cache_dir = $CI->config->item('cache');
    33         //$this->caching = true;
    34         //$this->cache_lifetime = ;
    35         //$this->debugging = true;
    36         $this->left_delimiter = '{#';
    37         $this->right_delimiter = '#}';
    38     }
    39 
    40     // --------------------------------------------------------------------
    41 
    42     /**
    43      * An encapsulation of display method in smarty class
    44      *
    45      * @access    public
    46      * @param    string
    47      * @param   mixed
    48      * @return    void
    49      */
    50     public function view($template_file, $assigns = array())
    51     {
    52         if (strpos($template_file, '.') === false)
    53         {
    54             $template_file .= '.html';
    55         }
    56 
    57         if ( ! is_file($this->template_dir . '/' . $template_file)) {
    58             show_error("Smarty error: {$template_file} cannot be found.");
    59         }
    60 
    61         if (is_array($assigns) && !empty($assigns))
    62         {
    63             foreach ($assigns as $key => $value)
    64                 $this->assign($key, $value);
    65         }
    66 
    67         $this->display($template_file);
    68     }
    69 }
    70 /* End of file Smarty.php */
    71 /* Location: ./application/libraries/Smarty.php */

    //================================================================
    配置完毕
    //================================================================
    使用方法:
    在控制器中如:

    View Code
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Test extends CI_Controller {
    
        /**
         * Test construtor
         */
        public function __construct()
        {
            parent::__construct();
                    $this->load->library('smarty');
        }
            /**
         * Index Page for this controller.
         */
            public function index()
        {        
            $this->cismarty->view('test',array('title'=>'测试'));
        }
    }

    在视图中如:

    View Code
     1 <html>
     2 <head>
     3     <title>{#$title#}</title>
     4 </head>
     5 <body>
     6 
     7 <p>work well</p>
     8 
     9 </body>
    10 </html>
  • 相关阅读:
    Linux安装Nginx
    CentOS7上安装google谷歌浏览器
    解决MYSQL的错误:Got a packet bigger than 'max_allowed_packet' bytes
    Mysql ERROR 1067: Invalid default value for 字段
    Mysql查看版本号的五种方式介绍
    linux下mysql 查看默认端口号与修改端口号方法
    yum 和 rpm安装mysql彻底删除
    SpringBootSecurity学习(06)网页版登录方法级别的权限
    SpringBootSecurity学习(05)网页版登录内存中配置默认用户
    SpringBootSecurity学习(04)网页版登录其它授权和登录处理
  • 原文地址:https://www.cnblogs.com/fredshare/p/2445840.html
Copyright © 2020-2023  润新知