天天看點

CodeIgniter內建Smarty

第一步:安裝CodeIgniter

<a href="http://codeigniter.com/downloads/" target="_blank">點選立即下載下傳最新版本的Zip包&gt;&gt;</a>

CodeIgniter內建Smarty

解壓後,複制檔案夾下面的application、system、index.php至項目根目錄中

CodeIgniter內建Smarty
CodeIgniter內建Smarty

第二步:安裝Smarty

<a href="http://www.smarty.net/download" target="_blank">點選下載下傳最新的Zip包&gt;&gt;</a>

在CodeIgniter的application目錄下的third_party目錄中建立一個名為smarty的目錄,将解壓出來的libs包複制到該目錄中。

CodeIgniter內建Smarty
CodeIgniter內建Smarty

第三步:建立模闆目錄

在application目錄的views目錄中建立兩個檔案夾templates、templates_c

第四步:編寫安裝代碼

Smarty.php(複制至appliction/libraries目錄中)

第五步:更新CodeIgniter配置

這裡隻是修改application/config/autoload.php檔案中的libraries項,讓頁面自動載入smarty。如果不在這裡配置,隻需在要用到smarty的地方顯示調用$this-&gt;load-&gt;library(‘smarty’);

第六步:運作執行個體

預設的例子是直接通路你的域名,比如這裡meteoric001.com/

CodeIgniter內建Smarty

或者使用:

meteoric001.com/index.php/welcome/

meteoric001.com/index.php/welcome

meteoric001.com/index.php/welcome/index

URL裡面帶個index.php可能不太好看,這裡修改一下伺服器配置(nginx為例)

CodeIgniter內建Smarty

最後寫一個名叫example的例子,運作效果

CodeIgniter內建Smarty

application/controllers/example.php

另外一個頁面模闆:

CodeIgniter內建Smarty

本文參考:

<a href="http://www.coolphptools.com/codeigniter-smarty" target="_blank">CodeIgniter+Smarty - Perfect Together</a>

<a href="http://codeigniter.org.cn/user_guide/general/urls.html" target="_blank">CodeIgniter URL</a>