先來看一下smarty的注釋方法
然后運行,查看一下源代碼
html和smarty的注釋區別就是源代碼中能看見html的注釋代碼,而看不到smarty里面的注釋代碼
再來看看,smarty中能讀字符串,看看能不能把數組讀出來
php文件中
<?php include("../init.inc.php"); $smarty->assign("ceshi","你好"); //注冊變量的方法 $arr = array(1,2,3,4,5); $smarty->assign("shuzu",$arr); $smarty->display("test.html");
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}> <{$shuzu[0]}> </body> </html>
看一下運行的結果
把數組成功取出來了
再來看取關聯數組
php文件
<?php include("../init.inc.php"); $smarty->assign("ceshi","你好"); //注冊變量的方法 $arr = array("one"=>"hello","two"=>"world"); $smarty->assign("shuzu",$arr); $smarty->display("test.html");
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}> <{$shuzu["one"]}> </body> </html>
運行一下
也成功的輸出了
smarty中也支持點語法,如圖
.one也可以成功輸出
取two的值就.two
取到的值
再來看看對象支不支持
<?php include("../init.inc.php"); $smarty->assign("ceshi","你好"); //注冊變量的方法 class Ren { public $name="zhangsan"; } $r=new Ren(); $smarty->assign("r",$r); $smarty->display("test.html");
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}> <{$r->name}> </body> </html>
運行后
說明這個變量是支持任何類型的
有一個標簽,可以讓里面的內容不參與解析
運行后
被原樣輸出了
這個標簽可以加在js或css外層,為了防止解析出錯
再來試一下,通過配置文件來做元素的樣式
配置文件是寫在這個目錄下的
自定義一個文件,censhi 配置文件的后綴都是.conf
里面隨便寫兩個樣式
然后還是用test.php和test.html 這兩個文件來做
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> <{config_load file='ceshi.conf'}> <!--調了一個函數,傳了一個參數--> <div style="width:100px; height:100px; background-color:<{#color#}>"> </div> </body> </html>
php文件中
<?php include("../init.inc.php"); $smarty->display("test.html");
運行后
在從配置文件中改個顏色試試
其他兩個文件的內容不用動
運行后
配置文件中還可以這樣寫
相當于把這些樣式分了一些類
來看一下配置文件中有兩個color該怎么取
如果取第二個的話在引入配置文件時后面還需要加上這樣一句話
運行后看看
取到了two下面的color,為紅色
還可以這樣改
運行后也是紅色
把上面改成one,那么取到的就是綠色
所以這兩種方法都可以用
{$smarty.const} 取常量
舉個例子來看看
php文件中
<?php include("../init.inc.php"); define("AA","nihao"); $smarty->display("test.html");
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> <{config_load file='ceshi.conf' section='one'}> <!--調了一個函數,傳了一個參數--> <div style="width:100px; height:100px; background-color:<{$smarty.config.color}>"> </div> <{$smarty.const.AA}> </body> </html>
看看運行的結果
取到了AA的值
所以獲取常量的方法就是
$smarty.const.常量名
這個運行后
輸出的是右分隔符
把r換成l試試
運行后
輸出的是左分隔符
再看一下取時間怎么取
php文件中
<?php include("../init.inc.php"); $sj=time(); $smarty->assign("sj","$sj"); $smarty->display("test.html");
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> <{$sj}> </body> </html>
先來運行一下
取到的是時間戳
php文件不用改變
html文件中要這樣寫才能把時間調出來
運行后
時間按照代碼的格式顯示出來
這是變量調節器,相當于一個函數
smarty可以支持自定義變量調節器
在這個目錄下有很多函數
modifier打頭的是變量調節器
function打頭的是函數
block打頭的是快函數
如果要使用自定義變量調節器的話,可以自己新建一個文件
這個文件就是新建的,要注意格式要對,modifier是打頭的,中間是隨便給他起的一個名字
然后在里面寫上簡單的內容,可以參照它原來有的變量調節器文件來寫
然后php文件中
html文件中
運行一下
mark標簽是標記的意思
如果把html文件中的mark去掉的話
運行后
就沒有黃色標記了
調節器文件中,至少有一個參數,多了不限
這個也可以把當前時間調出來
還是用原來的文件,把數據都改一下就可以
php文件不用改
html文件
運行后
當前時間調取成功
不含病毒。www.avast.com |
留言列表