smarty模板自定义变量

一、通过smarty方式调用变量调节器

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>


<!--显示当前时间戳-->
<{$smarty.now}>
	
<!--调用调节器显示想要的时间格式-->
<{$smarty.now|date_format:"%Y-%m-%d %H-%M-%S"}>

</body>
</html>

  分别显示:

  1498791289

   2017-06-30 04-54-49

 格式:变量 |  变量调节器的名称 参数  (竖线和冒号)

二、通过自定义方式调用变量

为什么要自己写呢?因为调用变量调节器需要自己去手册找到参数,不如自己写好直接调用。

1、写一个时间的变量调节器

(1)在plugin文件夹中新建一个文件:modifier.time.php

<?php
	//用来格式化时间日期
function smarty_modifier_time($str){
	return date("Y-m-d H:i:s",$str);
}
?>

 (2)test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

	<!--调用自定义的变量调节器-->
	<{$smarty.now|time}>

</body>
</html>

  显示效果:2017-06-30 05:06:53

什么时候用,就可以什么时候直接调取了,不用在找手册了呢

2、做一个截取字符串的变量调节器

(1)modifier.jiequ.php

<?php

function smarty_modifier_jiequ($str,$cd,$sl){
//	第一个参数:是传过来的变量,必须有
//	第二个参数:是截取多长
//	第三个参数:是要代替后面的省略符号
	
	$str = substr($str,0,$cd);
	return $str.$sl;
}
?>

 (2)12.php

<?php
	header("content-type:text/html;charset=utf-8");
	//引入smarty类
require "../init.inc.php";

//数组类型
$arr =array("one"=>"1111","two"=>"2222");


//注册变量
$smarty->assign("ceshi","我叫你好你叫遇见他叫断桥这是真的么");  


$smarty->assign("haha","12345678901234567890");


$smarty->assign("nnn","abcdefghijklmnopqrstuvwxyz");
//显示
$smarty->display("test.html");
?>

  

(3) test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
	
		<{$ceshi|jiequ:"12":"..."}>
			
		<{$haha|jiequ:"10":"..."}>
			
		<{$nnn|jiequ:"10":"..."}>
		

</body>
</html>

 分别显示:

	我叫你好...
			
	123456789012...
			
	abcdefghijkl...

注意:汉字在php中相当于三个字符;所以当输出为汉字时要注意截取的长度:

 3、做一个与数据库相关的变量调节器(zhangsan------张三)

(1)12.php

<?php
header("content-type:text/html;charset=utf-8");	
//先引入DBDA类
require "DBDA.class.php";
//引入smarty类
require "../init.inc.php";

//从订单表中找到用户名uid
$db = new DBDA();
$sql = "select uid from orders";
$arr = $db->query($sql);

//将得到的uid注册
$smarty->assign("one",$arr[0]);

$smarty->display("test.html");
?>

  

  (2)modifier.uername.php

<?php
function smarty_modifier_username($str)
{
	//无法直接调用DBDA类,所以采用最原始的方法
	$sql = "select name from users where uid='zhangsan'";
	$db = new mysqli("localhost","root","","book");
	$result = $db->query($sql);

	$arr = $result->fetch_row();
	return $arr[0];
}
?>

(3) test.html 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
	
		<{$one[0]|username}>
		

</body>
</html>

 输出汉字:张三

可以做很多这样的自定义的调节器,便于以后调用~~

原文地址:https://www.cnblogs.com/chenguanai/p/7098419.html