Smarty学习笔记

模板文件smarty/templates/test.tpl:


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Smarty学习笔记</title>
</head>

<body>
{include file="test_header.tpl" xxx="Smarty包含文件可自定义属性和值"} 

<h1>{$newstitle|capitalize} <!-- 将变量值首字母转大写 --></h1>
<p>{$newstitle|cat:"hi,":"Simon"} <!-- cat为连接字符串,后面跟冒号: --></p> 
<p>{$time} <!-- 直接显示时间戳 --></p>
<p>{$time|date_format:"%Y-%m-%d %H:%M:%S"} <!-- 将时间戳转成日期时间格式 --></p> 
<p>{$notitle|default:"没有内容!"} <!-- 当变量的值为空时,设置默认显示 --></p>
<p>{$siteurl|escape:"url"} <!-- 转码 后面的参数可以是url,html,hex,hexentity... --></p>
<p><a href="mailto:{$email|escape:'hex'}">{$email|escape:"hexentity"}</a> <!-- 转码 后面的参数可以是url,html,hex,hexentity... --></p>
<p>{$myname|upper} <!-- 转大写 --></p>
<p>{$myname2|lower} <!-- 转小写 --></p>
<p>{$newline|nl2br} <!-- 换行处理 --></p>
<p>{$cutstr|truncate:12} <!-- 截取字符串,末尾加省略号 --></p>
<p>
{if $realname eq "Simon"}
Welcome Simon.
{elseif $realname eq "Wilma"}
Welcome Wilma.
{else}
Welcome,whatever you are.
{/if}<!-- eq相当于==、neq相当于!=、gt相当于>、lt相当于< -->
</p>
<p>
{section name=sn loop=$person_list}
	第{$smarty.section.sn.index+1}行    {$person_list[sn].id}    {$person_list[sn].name}    {$person_list[sn].sex}
    {if $smarty.section.sn.first}    <span style="color:red;">new</span>{/if}
    {if $smarty.section.sn.last}    <span style="color:blue;">last</span>{/if}
    <br>
{/section}
<!--
除了name和loop属性外,还有以下属性:
start 循环执行的初始位置,如果该值为负数,开始位置从数组的尾部算起。
例如:如果数组中有7个元素,指定start为-2,那么指向当前数组的索引为5。
非法值(超过了循环数组的下限)将被自动调整为最接近的合法值。
step  该值决定循环的步长,例如指定step=2将只遍历下标为0、2、4等的元素。
如果step为负值,那么遍历数组的时候从后向前遍历。
max   设定循环最大执行次数。
show  决定是否显示该循环。 
 -->
</p>
<p>
{foreach item=person from=$person_list2} 
{$person.id}    {$person.name}    {$person.sex}<br>
{foreachelse} 
当前没有记录
{/foreach}
</p>
<p>
{$myobj->meth1(array("苹果","熟了"))} <!-- 类在模板中的使用 -->
</p>
<p>{"Y-m-d"|date:$add_time} <!-- php内置函数date在模板中的使用 "Y-m-d"作为第一个参数传到date函数里,冒号后面的作为第二个,第三个...参数传递--> <!-- php内置函数在模板中的通用格式:变量|php内置函数:参数2的参数值:参数3的参数值 --></p>
<p>{"b"|str_replace:"123":$replacer} <!-- php内置函数str_replace在模板中的使用 --></p>
<p>{f_test p1="Simon" p2="Wilma" }</p> <!-- f_test调用自定义函数test,后面的p1,p2...为参数,名称和个数不限  -->
<p>{hellotest p1="hello!" p2=" Simon"} <!-- 自定义smarty function插件,相当于调用自定义函数 在smarty/plugins/新建function.hellotest.php文件,自定义smarty_function_hellotest函数 --></p>
<p>{$times|timetest:"Y-m-d H:i:s"}</p> <!-- 自定义smarty modifier变量调节器插件, 在smarty/plugins/新modifier.timetest.php文件,自定义smarty_modifier_timetest函数 -->
<p>
{replacetest replace="true" maxnum=30}
	{$tmpstr}
{/replacetest}
<!-- 自定义smarty block插件, 在smarty/plugins/新block.replacetest.php文件,自定义smarty_block_replacetest函数 --></p>
</body>
</html>


模板头部包含文件:smarty/templates/test_header.tpl


{$xxx}


模板程序/test.php:


<?php
	require_once(dirname(__FILE__)."/smarty/smarty.php");	 //引入配置文件

	//*****Smarty学习笔记*****
	$str="hello world!";	 //定义变量
	$smarty->assign('newstitle',$str);	//替换掉模版中的{$str}
	$smarty->assign('time',time());
	$smarty->assign('notitle',""); //当变量的值为空时 
	$smarty->assign('siteurl',"http://www.csdn.net/"); //url转码
	$smarty->assign('email',"vsiryxm@qq.com");
	$smarty->assign('myname',"my name is simon"); //转大写
	$smarty->assign('myname2',"MY NAME IS SIMON"); //转小写
	$smarty->assign("newline","my 
	name 
	is 
	simon"); //换行处理
	$smarty->assign('cutstr',"MY NAME IS SIMON"); //截取字符串
	$smarty->assign('realname',"Simon"); //if判断 
	$person_list = array(
		array("id"=>1,"name"=>"Simon","sex"=>"男"),
		array("id"=>4,"name"=>"Wilma","sex"=>"女"),
		array("id"=>5,"name"=>"Tom","sex"=>"男")
	); 
	$smarty->assign('person_list',$person_list); //section循环
	$smarty->assign('person_list2',$person_list); //foreach循环 
	
	//类在模板中的使用
	class My_Object
	{
		function meth1($params) 
		{
			return $params[0]."已经".$params[1];
		}
	}
	$myobj = new My_Object();
	$smarty->assign('myobj',$myobj);
	$smarty->assign('add_time',time()); //php内置函数在模板中的使用,通用格式:{变量|php内置函数:参数2的参数值:参数3的参数值}
	$smarty->assign('replacer',"abcdefg");
	
	//自定义函数在模板中的使用
	function test($params)
	{
	   $p1 = $params['p1']; 
	   $p2 = $params['p2'];
	   return "传入的参数1值为".$p1.",传入的参数2值为".$p2;
	}
	$smarty->registerPlugin("function","f_test","test"); //smarty中使用registerPlugin方法注册自定义函数
	$smarty->assign('times',time());
	$smarty->assign('tmpstr',"Hello,my name is HanMeimei。How are you?");
	$smarty->display("test.tpl");	//显示模版文件夹中的test.tpl
?> 


function插件文件:smarty/plugins/function.hellotest.php


<?php 

function smarty_function_hellotest($params) //注意函数命名 smarty_function_开头
{
	$p1 = $params["p1"];
	$p2 = $params["p2"];
	return $p1.$p2;
}

?>


modifier插件文件:smarty/plugins/modifier.timetest.php


<?php 

function smarty_modifier_timetest($time,$format) //注意函数命名 smarty_modifier_开头,这里参数与function有区别,不再是数组形式
{ 
	return date($format,$time);
}

?>


block插件文件:smarty/plugins/block.replacetest.php


<?php 

function smarty_block_replacetest($params,$content) //注意函数命名 smarty_block_开头,这里参数与function、modifier有区别,不再是数组、单个参数形式,是综合形式  $params是数组,$content为模板中{$tmpstr}的内容
{
	$replace=$params["replace"];
	$maxnum = $params["maxnum"];  
	if($replace=="true") 
	{
		$content = str_replace(",",",",$content);
		$content = str_replace("。",".",$content); 
	}
	$content = substr($content,0,$maxnum);
	return $content;
}

?>


学习文件包下载>>





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值