Smarty 安装配置信息以及简单的使用

Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,

用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目的就是要使PHP程序员同前端人员分离,使程序员改变程序的逻辑内容不会影响到前端

人员的页面设计,前端人员重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。

//首先,那当然是引入 smarty 的类文件

include_once("smarty/lib/Smarty.class.php"); //包含smarty类文件
$smarty = new Smarty(); //实例对象$smarty

$smarty->config_dir="Smarty/Config_File.class.php";  // 目录变量

$smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存

$smarty->template_dir = "./templates"; //设置模板目录

$smarty->compile_dir = "./templates_c"; //设置编译目录

$smarty->cache_dir = "./smarty_cache"; //缓存文件夹

//smarty缓存的配置

$smarty->cache_dir = "/caches/";  //缓存目录

$smarty->caching = true;  //开启缓存,为flase的时侯缓存无效

$smarty->cache_lifetime = 60;  //缓存时间

$smarty->display('cache.tpl', cache_id);  //创建带ID的缓存

$smarty->clear_all_cache(); //清除所有缓存

$smarty->clear_cache('index.htm'); //清除index.tpl的缓存

$smarty->clear_cache('index.htm',cache_id); //清除指定id的缓存

smarty 的使用

$smarty->assign("模板变量('name')", "值(数组/变量)");
$smarty->display("模板名称");

//模板中
<title>{$name}</title>

变量操作符使用语法
{$name|capitalize }

包含文件
{include file="header.htm"}

避免 页面中的js与smarty 冲突

{literal}  
 <script language=javascript>
    ……………………………………
 </script>
{/literal}

if 判断

{if $type==1}

{else}

{/if}

foreach 循环

{foreach from=$name item=id}
{$id}
{/foreach}

//或

{foreach key=j item=v from=$name }
{$j}: {$v}
{/foreach}

非特殊说明,本博所有文章均为博主原创。

备注:相关侵权、举报、投诉及建议等,请联系站长

添加新评论

昵称
邮箱
网站