CI框架整合smarty步驟詳解
來源:易賢網(wǎng) 閱讀:718 次 日期:2016-08-20 14:05:16
溫馨提示:易賢網(wǎng)小編為您整理了“CI框架整合smarty步驟詳解”,方便廣大網(wǎng)友查閱!

本文詳細講述了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程序設計有所幫助。

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:CI框架整合smarty步驟詳解
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)