配置(新建smarty_inc.php):
这样配置后,逻辑代码(index.php)就与样式(index.html)分离了。
原型是:{section name = name loop = $varName[, start = $start, step = $step, max = $max, show = true]}
name: section的名称,不用加$
include_once ('Smarty/Smarty.class.php');
$smarty=new Smarty();//实例化对象
$smarty->config_dir="Smarty/Config_File.class.php";//目录变量
$smarty->caching=false;//是否使用缓存
/*可选↑↑必选↓↓*/
$smarty->template_dir="./template";//设置模板目录
$smarty->compile_dir="./templates_c";//设置编译目录
$smarty->cache_dir="./smarty_cache";//缓存文件夹
$smarty->left_delimiter="{";
$smarty->right_delimiter="}";
//========================================
$smarty->assign("name","kino");//进行模板变量替换
$smarty->display("index.html");
使用配置(新建index.php)
include "smarty_inc.php";
$name="kino";
$smarty->assign("title",$name);//进行模板变量替换
$smarty->display("index.html");//样式显示文件
这样配置后,逻辑代码(index.php)就与样式(index.html)分离了。
SECTION语句(使用数组)
(index.php)
$name[]=array("name"=>"我爱你","date"=>"2012-12-12");
$name[]=array("name"=>"你爱他","date"=>"2012-12-12");
$name[]=array("name"=>"他爱她","date"=>"2012-12-12");
$name[]=array("name"=>"她爱他","date"=>"2012-12-12");
然后在index.html内
{section name=list loop=$title}
<h2>
{$title[list].name} - {$title[list].date}
</h2>
{/section}
原型是:{section name = name loop = $varName[, start = $start, step = $step, max = $max, show = true]}
name: section的名称,不用加$
$loop: 要循环的变量,在程序中要使用assign对这个变量进行操作。
$varName:数组名
$start: 开始循环的下标,循环下标默认由0开始
$step: 每次循环时下标的增数
$max: 最大循环下标
$show: boolean类型,决定是否对这个块进行显示,默认为true
常用变量操作符(函数)
/*=========================
capitalize [首字母大写]
count_characters [计算字符数]
cat [连接字符串]
count_paragraphs [计算段落数]
count_sentences [计算句数]
count_words [计算词数]
date_format [时间格式]
default [默认]
escape [转码]
indent[缩进]
lower[小写 ]
nl2br[换行符替换成<br />]
regex_replace[正则替换]
replace[替换]
spacify[插空]
string_format[字符串格式化]
strip[去除(多余空格)]
strip_tags[去除html标签]
truncate[截取]
upper[大写]
wordwrap[行宽约束](多少个字符后自动换行)
===========================*/
内置函数
=======无键值数组
{foreach from=$name item=id}
{/foreach}
=======有键值数组
{foreach key=j item=v from=$name } // key 键值 from 数组 item 内容
{/foreach}
{literal} 可以放JS脚本等包含‘{’的语句 {/literal}
{if} 判断 {/if}
{strip} 处理多余空格 {/strip}