php smarty

现在用smarty模板的应该不多,但还是要了解一下它的运作模式。

一、安装
下载Smarty文件放到你们站点中
然后在站点的index.php 文件中的代码:
<?php
include_once("./Smarty/Smarty.class.php"); //包含smarty类文件

$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->templates("./templates"); //设置模板目录
$smarty->templates_c("./templates_c"); //设置编译目录
$smarty->cache("./cache"); //缓存目录
$smarty->cache_lifetime = 0; //缓存时间
$smarty->caching = true; //缓存方式

$smarty->left_delimiter = "{#";
$smarty->right_delimiter = "#}"; 
$smarty->assign("name", "zaocha"); //进行模板变量替换
$smarty->display("index.htm"); //编译并显示位于./templates下的index.htm模板
?>


二、模板部分
index.htm文件中代码:

//包含公共文件
{include file='header.htm'}

//输出变量
{$name}

//遍历输出数据
{foreach from=$list key=key item=value}
 名字:{$value.name}
{foreachelse}
            没有您所需要的数据!
{/foreach}

//js、css代码,需要用{literal}标志
{literal}
<script>##代码</script>
<style>##代码</style>
{/literal}

//逻辑判断
{if $value.sex == 0}
 性别:女
{else}
 性别:男
{/if}

猜你喜欢

转载自xiaoyu-91.iteye.com/blog/2283087