一、通過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)test.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>
輸出漢字:李四
可以做很多這樣的自定義的調節器,便於以后調用~~
3、做一個與數據庫相關的變量調節器(zhangsan------張三)
