Welcome 微信登录

首页 / 网页编程 / PHP / smarty配置以及变量调节器详解

smarty配置以及变量调节器详解2014-04-06最近没事儿做,就研究研究smarty模版引擎,越来越觉得smarty的强大了,smarty的教程在网上好像都比较乱。

1.下载smarty,http://www.smarty.net/download

2.把下载下来的smarty改名为smarty然后复制到新建好的文件夹里

3.新建一个smarty.inc.php(也可以是别的)

<?phprequire_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");//加载functionif (file_exists("common/common.php")){require_once "common/common.php";}
并且手动建立相应的目录,调试的时候建议把缓存关了。

然后新建一个index.php,

<?phprequire_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");