日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Smarty模板技术学习

發(fā)布時間:2024/7/23 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Smarty模板技术学习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

模板引擎技術(shù):使得php代碼和html代碼分離的技術(shù)就稱為“模板引擎技術(shù)”

自定義smarty模板技術(shù)實(shí)現(xiàn)

<?php//迷你smarty原理 class MiniSmarty{public $var_arr = array();public $template_dir = "./view/";public $compile_dir = "./view_c/";//把外部聲明變量設(shè)置為當(dāng)前類內(nèi)部的成員屬性信息var_arrfunction assign($k,$v){$this ->var_arr[$k] = $v;}//顯示模板內(nèi)容function display($tpl){include $this->compile($tpl);}//把html模板內(nèi)容引入,替換{ ---------》< ?php echo// } ---------》 ; ? >//模板編譯,把{}編譯為php標(biāo)簽內(nèi)容//$tpl: 被編譯模板文件的名稱function compile($tpl){//$tpl = "order.html";$tpl_file = $this -> template_dir.$tpl; //模板文件$com_file = $this->compile_dir.$tpl.".php"; //編譯文件//判斷編譯文件是否存在,如果存在直接調(diào)用,并且該編譯文件生成后對應(yīng)的模板文件沒有再修改//正常情況,編譯文件的時間稍大,模板文件時間相對小一些if(file_exists($com_file) && (filemtime($tpl_file)<filemtime($com_file))){return $com_file;exit;}//把$tpl內(nèi)容抓取出來,替換內(nèi)部的{}內(nèi)容$cont = file_get_contents($tpl_file);//替換{ ---------》< ?php echo//$cont = str_replace("{","< ?php echo ",$cont);//在模板中調(diào)用的變量即是 當(dāng)前對象的屬性var_arr的元素信息//< ?php echo $this->var_arr['name']; ? >$cont = str_replace("{\$","<?php echo \$this->var_arr['",$cont);//} ---------》 ; ? >//$cont = str_replace("}","; ? >",$cont);$cont = str_replace("}","']; ?>",$cont);//把生成好的內(nèi)容放入一個文件里邊,然后引入之file_put_contents($com_file, $cont);//引入$com_file編譯文件return $com_file;} }

通過MySmarty對Smarty進(jìn)行初始化設(shè)置

<?php//在這個類里邊設(shè)置smarty公共配置 include "./libs/Smarty.class.php";class MySmarty extends Smarty{function __construct(){parent::__construct(); //先執(zhí)行父類的構(gòu)造函數(shù),避免被覆蓋//更改smarty的左右標(biāo)記$this -> left_delimiter = "{";$this -> right_delimiter = "}";$this -> setTemplateDir('.' . DS . 'view' . DS);//修改模板目錄$this -> setCompileDir('.' . DS . 'view_c' . DS);//修改編譯文件目錄} }

smarty的三種變量使用

1.smarty?>assign();2.數(shù)使_GET POST_SESSION COOKIE_ENV $_SERVER等等

<body>當(dāng)前用戶:{$smarty.session.username}<br />名字:{$name}<br />顏色:{$color}<br />地區(qū):{$smarty.get.addr}<br />年齡:{$smarty.get.age}<br />{$smarty.now}{*當(dāng)前時間戳信息*}<br />{$smarty.const.HOST}{*獲得常量信息*}<br />{$smarty.template}{*當(dāng)前請求的模板*}<br />{$smarty.current_dir}{*當(dāng)前模板路徑*}<br />{$smarty.version}<br />{$smarty.ldelim},{$smarty.rdelim} {*左右標(biāo)記信息*}<br /></body>

3.配置變量信息
通過配置變量的定義 和 段 的設(shè)置可以輕松實(shí)現(xiàn)同一頁面根據(jù)用戶不同喜好顯示不同的樣式。

定義

[shop] POLICE=京公網(wǎng)安備110000000011 NETWORK=互聯(lián)網(wǎng)出版許可證 BROADCAST="廣播電視節(jié)目制作經(jīng)營許可證 (京)字第434號"[car] POLICE=京公網(wǎng)安備973498378 NETWORK=互聯(lián)網(wǎng)出版許可證02 BROADCAST="廣播電視節(jié)目制作經(jīng)營許可證 (京)字第978號"

通過以下語句引入配置
{config_load file=”site.conf” section=”car”}

<body>名字:{$name}<br />顏色:{$color}<br /><h2>顯示相關(guān)的許可信息</h2>{#POLICE#}<br />{#NETWORK#}<br />{#BROADCAST#}<br /></body>

{}標(biāo)記沖突的問題

1.把Smarty的標(biāo)記變?yōu)槠渌麡?biāo)記
2.把{}符號變?yōu)椴煌?
3.利用literal標(biāo)簽把有{}的內(nèi)容給括起來

數(shù)組/對象變量的使用

數(shù)組:數(shù)組[下標(biāo)] 或 數(shù)組.下標(biāo)
對象:對象->成員

<?php include "./MySmarty.class.php";$smarty = new MySmarty();//索引數(shù)組 $smarty -> assign('fruit',array('banana','pear','apple','grape')); //關(guān)聯(lián)數(shù)組 $smarty -> assign('animal',array('north'=>'tiger','sichuan'=>'panda','helan'=>'pig')); $smarty -> assign('student',array('first'=>array('tom','jack','mary')));$smarty -> display('07.html');

訪問

<body><h2>訪問數(shù)組元素信息(索引)</h2><div>{$fruit[2]}<br />{$fruit[3]}<br />{$fruit.0}<br />{$fruit.1}<br /></div><h2>訪問數(shù)組元素信息(關(guān)聯(lián))</h2><div>{$animal['helan']}<br />{$animal.north}<br /></div><h2>訪問數(shù)組元素信息(二維)</h2><div>{$student.first.1}<br />{$student['first'][2]}<br /></div></body>

訪問對象

<?php include "./MySmarty.class.php";$smarty = new MySmarty();class Person{public $name = "xiaoming";public function run(){return "正在跑步。。。";} }$per = new Person;$smarty -> assign('per',$per);$smarty -> display('08.html');

在HTML中訪問

<body><h2>訪問對象信息</h2>{$per -> name}<br />{$per -> run()}<br /></body>

遍歷數(shù)組信息foreach/section

{foreach 數(shù)組 as k=>v}
@iteration 自然數(shù)1開始序號
@index 0開始序號
@first 如果第一個項(xiàng)目則返回1,否則返回false
@last 如果最后一個項(xiàng)目則返回1,否則返回false
@show 判斷數(shù)組是否為空
@total 數(shù)組的元素個數(shù)

<body><h2>數(shù)組遍歷</h2>{*元素值@iteration 獲得1開始的自然數(shù)序號元素值@index 0開始的序號信息元素值@first 如果是第一個元素返回1,否則返回0元素值@last 如果是最后一個元素返回1,否則返回0元素值@total 返回?cái)?shù)組元素的總個數(shù)元素值@show 判斷當(dāng)前數(shù)組是否為空*}<div>{foreach $fruit as $k => $v}{$v@first}--{$v@index}--{$v@iteration}--{$k}--->{$v}--{$v@last}<br />{foreachelse}沒有任何記錄信息<br />{/foreach}當(dāng)前數(shù)組的總個數(shù):{$v@total}<br />當(dāng)前數(shù)組是否為空:{$v@show}</div><h2>遍歷應(yīng)用(第一個元素顯示特殊背景色)</h2><div>{foreach $animal as $kk => $vv}{if $vv@first == 1}<p style='background-color:lightblue;'>{/if}{$kk}---->{$vv}<br />{if $vv@first == 1}</p>{/if}{/foreach}</div><h2>遍歷應(yīng)用(隔行顯示不同顏色)</h2><div>{foreach $animal as $kkk => $vvv}{if $vvv@iteration%2 == 0}<p style='background-color:lightgreen;'>{/if}{$kkk}---->{$vvv}<br />{if $vvv@iteration%2 == 0}</p>{/if}{/foreach}</div></body>

二維數(shù)組遍歷

<body><h2>二維數(shù)組遍歷</h2><div>{foreach $student as $k => $v}{foreach $v as $kk => $vv}{$kk}--》{$vv}<br />{/foreach}{/foreach}</div></body>

section遍歷

{section name=”名稱” loop=$animal start=2 step=2 max=5 show=false}{$animal[名稱]}關(guān)鍵字: {$smarty.section.名稱.first}{$smarty.section.名稱.last}{$smarty.section.名稱.iteration}{$smarty.section.名稱.total} {/section }

foreach和section最大的區(qū)別是:
foreach可以遍歷索引和關(guān)聯(lián)數(shù)組
section只給遍歷索引數(shù)組

for循環(huán)

<body><h2>for循環(huán)語句</h2><div>{for $i=1 to 10 step=3}{$i}<br />{/for}<hr />{*for($m=100 ; $m<=90; $m++)*}{for $m=100 to 90 step=-2 max=4}{$m}<br />{/for}</div></body>

if分支

<body><h2>if分支結(jié)構(gòu)語句</h2>{* if else elseif *}<div>{if $age>0 && $age<10}兒童<br />{elseif $age>=10 && $age<20}少年<br />{elseif $age>=20 && $age<30}青年<br />{elseif $age>=30}成年<br />{/if}<hr />{if $age+10>=30}10年之后就成年了<br />{/if}</div></body>

復(fù)選框、單選按鈕、下拉列表

<?php include "./MySmarty.class.php";$smarty = new MySmarty();$smarty -> assign('hobby_out',array('籃球','足球','排球','棒球')); $smarty -> assign('hobby_val',array('a','b','c','d'));$smarty -> assign('val_out',array('A'=>'籃球','B'=>'足球','C'=>'排球','D'=>'棒球'));$smarty -> assign('sel',array('A','C','D'));$smarty -> display('13.html'); <body><h2>復(fù)選框設(shè)置</h2><div>{*<input type="checkbox" name="hobby" value='1'>籃球*}{*<input type="checkbox" name="hobby" value='2'>棒球*}{html_checkboxes name="hobby" options=$val_out selected=$sel separator="<br />" label_ids=true}</div></body>

下拉列表

<?php include "./MySmarty.class.php";$smarty = new MySmarty();$smarty -> assign('val_out',array('0'=>'請選擇','A'=>'小學(xué)','B'=>'初中','C'=>'高中','D'=>'大學(xué)')); $smarty -> assign('sel',array('A','D'));$smarty -> assign('val_out1',array('man','girl','secret'));$smarty -> display('14.html'); <body><h2>下拉列表設(shè)置</h2><div>{html_options name="xueli" options=$val_out multiple="multiple" selected=$sel}<hr /><select name="sex" ><option value="0">請選擇</option>{html_options options=$val_out1}</select></div></body>

總結(jié)

以上是生活随笔為你收集整理的Smarty模板技术学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。