最近沒事兒做,就研究研究smarty模版引擎,越來越覺得smarty的強大了,smarty的教程在網上好像都比較亂。
1.下載smarty,http://www.smarty.net/download
2.把下載下來的smarty改名為smarty然後復制到新建好的文件夾裡
3.新建一個smarty.inc.php(也可以是別的)
<?php
require_once 'smarty/Smarty.class.php';
$smarty=new Smarty();
$smarty->template_dir='templates/';
$smarty->compile_dir='templates_c/';
$smarty->cache_dir='temp/';
$smarty->config_dir='configs/';
$smarty->caching=0; //緩存
$smarty->cache_lifetime=60;
if (!defined('SITE_URL')){
$url=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
define('SITE_URL', $url);
}
if (!defined('__ROOT__')){
define('__ROOT__', rtrim(dirname(__FILE__),'\\').'\\');
}
//去除反斜槓
if (get_magic_quotes_gpc()){
function stripcslashes_arr($array){
is_array($array)?array_map('srtipcslashes_arr', $array):stripcslashes($array);
}
$_POST=stripcslashes_arr($_POST);
$_GET=stripcslashes_arr($_GET);
$_REQUEST=stripcslashes_arr($_REQUEST);
$_COOKIE=stripcslashes_arr($_COOKIE);
$_SESSION=stripcslashes_arr($_SESSION);
}
//時區設置
date_default_timezone_set('PRC');
//加載function
if (file_exists('common/common.php')){
require_once 'common/common.php';
}
並且手動建立相應的目錄,調試的時候建議把緩存關了。
然後新建一個index.php,
<?php
require_once 'smarty.inc.php';
$title='第一個標題';
$smarty->assign('title',$title);
$people=array(
array('name'=>'張三','sex'=>'男','age'=>'20'),
array('name'=>'李四','sex'=>'男','age'=>'40'),
array('name'=>'王五','sex'=>'女','age'=>'32'),
);
$smarty->assign('people',$people);
$smarty->display('index.tpl');