Smarty模板学习笔记

配置(新建smarty_inc.php):
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}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值