CI框架整合smarty步驟詳解

字號:


    本文詳細(xì)講述了CI框架整合smarty步驟。分享給大家供大家參考,具體如下:
    Ci結(jié)合smarty的配置步驟:
    1. 第一步配置ci和下載smarty的模板個人喜歡用(Smarty-3.1.8)這個版本。
    2. 第二部把下載到的smarty版本解壓然后把里面的libs文件改名為smarty然后把這個文件拷到ci\application\libraries目錄下面
    3. 在ci\application\libraries這個目錄下面建立一個文件,文件名可以自定義,例如見一個tp.php的文檔。
    4. 用編譯器打開tp.php然后寫入以下代碼:
    <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    require_once('smarty/smarty.class.php');
    class Tp extends Smarty{
     function tp(){
      parent::Smarty();
      $this->template_dir = APPPATH.'views';
      $this->compile_dir = APPPATH.'templates_c/';
      $this->left_delimiter = '<{';
      $this->right_delimiter = '}>';
     }
    }
    5. 在建立一個ci\application\templates_c文件夾
    6. 打開ci\application\config\autoload.php文件把
    $autoload['libraries'] = array();
    改成:
    $autoload['libraries'] = array('database','tp');
    OK我們的配置到這里就已經(jīng)成功了,接下來我們開始測試
    測試的第一步先建立一個控制器:
    1. 在\application\controllers下建立一個文件名為ceshi.php的文件,文件內(nèi)容
    <?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Home extends CI_Controller {
      function __construct()
      {
       parent::__construct();
       $this->load->helper('url');
       $this->tp->assign('base_url', base_url());
       //定義css以及js的路徑
      }
      function index()
      {
       $this->tp->assign("title","恭喜你smarty安裝成功!");
       $this->tp->assign("body","歡迎使用smarty模板引擎");
       $arr = array(1=>'zhang',2=>'xing',3=>'wang');
       $this->tp->assign("myarray",$arr);
       $this->tp->display('ceshi.html');
      }
    }
    2.建立模板文件在ci\application\views目錄下建立文件名為ceshi.html的文件,文件內(nèi)容為
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <script src='<!--{$base_url}-->js/jQuery.min.js' type='text/JavaScript' ></script>
     <link href="<!--{$base_url}-->css/login.css" rel="stylesheet" type="text/css" />
     <title>smarty安裝測試</title>
    </head>
    <body>
    <h1><{$title}></h1>
    <p><{$body}></p>
    <ul>
      <{foreach from=$myarray item=v}>
      <li><{$v}></li>
      <{/foreach}>
    </ul>
    </body>
    </html>
    最后輸入地址http://localhost/ci/application/index.php/ceshi (主意ci代表的是你自己放置ci框架中文件的根目錄)運行以后你將會看到你配置smarty成功的頁面,到這里ci和smarty的整合以及測試就完工了
    希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。