smarty模板自定义变量

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!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

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

 (2)test.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!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

1
2
3
4
5
6
7
8
9
10
11
<?php
 
function smarty_modifier_jiequ( $str , $cd , $sl ){
//  第一个参数:是传过来的变量,必须有
//  第二个参数:是截取多长
//  第三个参数:是要代替后面的省略符号
     
     $str = substr ( $str ,0, $cd );
     return $str . $sl ;
}
?>

 (2)12.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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

1
2
3
4
5
6
7
8
9
10
11
12
<?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 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!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/Liangbingbing/p/7153844.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值