Smarty模板技术学习
模板引擎技術(shù):使得php代碼和html代碼分離的技術(shù)就稱(chēng)為“模板引擎技術(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)前類(lèi)內(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: 被編譯模板文件的名稱(chēng)function compile($tpl){//$tpl = "order.html";$tpl_file = $this -> template_dir.$tpl; //模板文件$com_file = $this->compile_dir.$tpl.".php"; //編譯文件//判斷編譯文件是否存在,如果存在直接調(diào)用,并且該編譯文件生成后對(duì)應(yīng)的模板文件沒(méi)有再修改//正常情況,編譯文件的時(shí)間稍大,模板文件時(shí)間相對(duì)小一些if(file_exists($com_file) && (filemtime($tpl_file)<filemtime($com_file))){return $com_file;exit;}//把$tpl內(nèi)容抓取出來(lái),替換內(nèi)部的{}內(nèi)容$cont = file_get_contents($tpl_file);//替換{ ---------》< ?php echo//$cont = str_replace("{","< ?php echo ",$cont);//在模板中調(diào)用的變量即是 當(dāng)前對(duì)象的屬性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)容放入一個(gè)文件里邊,然后引入之file_put_contents($com_file, $cont);//引入$com_file編譯文件return $com_file;} }通過(guò)MySmarty對(duì)Smarty進(jìn)行初始化設(shè)置
<?php//在這個(gè)類(lèi)里邊設(shè)置smarty公共配置 include "./libs/Smarty.class.php";class MySmarty extends Smarty{function __construct(){parent::__construct(); //先執(zhí)行父類(lèi)的構(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(名稱(chēng),值);2.超級(jí)全局數(shù)組變量使用_GET POST_SESSION COOKIE_ENV $_SERVER等等
<body>當(dāng)前用戶(hù):{$smarty.session.username}<br />名字:{$name}<br />顏色:{$color}<br />地區(qū):{$smarty.get.addr}<br />年齡:{$smarty.get.age}<br />{$smarty.now}{*當(dāng)前時(shí)間戳信息*}<br />{$smarty.const.HOST}{*獲得常量信息*}<br />{$smarty.template}{*當(dāng)前請(qǐng)求的模板*}<br />{$smarty.current_dir}{*當(dāng)前模板路徑*}<br />{$smarty.version}<br />{$smarty.ldelim},{$smarty.rdelim} {*左右標(biāo)記信息*}<br /></body>3.配置變量信息
通過(guò)配置變量的定義 和 段 的設(shè)置可以輕松實(shí)現(xiàn)同一頁(yè)面根據(jù)用戶(hù)不同喜好顯示不同的樣式。
定義
[shop] POLICE=京公網(wǎng)安備110000000011號(hào) NETWORK=互聯(lián)網(wǎng)出版許可證 BROADCAST="廣播電視節(jié)目制作經(jīng)營(yíng)許可證 (京)字第434號(hào)"[car] POLICE=京公網(wǎng)安備973498378號(hào) NETWORK=互聯(lián)網(wǎng)出版許可證02 BROADCAST="廣播電視節(jié)目制作經(jīng)營(yíng)許可證 (京)字第978號(hào)"通過(guò)以下語(yǔ)句引入配置
{config_load file=”site.conf” section=”car”}
{}標(biāo)記沖突的問(wèn)題
1.把Smarty的標(biāo)記變?yōu)槠渌麡?biāo)記
2.把{}符號(hào)變?yōu)椴煌?
3.利用literal標(biāo)簽把有{}的內(nèi)容給括起來(lái)
數(shù)組/對(duì)象變量的使用
數(shù)組:數(shù)組[下標(biāo)] 或 數(shù)組.下標(biāo)
對(duì)象:對(duì)象->成員
訪問(wèn)
<body><h2>訪問(wèn)數(shù)組元素信息(索引)</h2><div>{$fruit[2]}<br />{$fruit[3]}<br />{$fruit.0}<br />{$fruit.1}<br /></div><h2>訪問(wèn)數(shù)組元素信息(關(guān)聯(lián))</h2><div>{$animal['helan']}<br />{$animal.north}<br /></div><h2>訪問(wèn)數(shù)組元素信息(二維)</h2><div>{$student.first.1}<br />{$student['first'][2]}<br /></div></body>訪問(wèn)對(duì)象
<?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中訪問(wèn)
<body><h2>訪問(wèn)對(duì)象信息</h2>{$per -> name}<br />{$per -> run()}<br /></body>遍歷數(shù)組信息foreach/section
{foreach 數(shù)組 as k=>v}
@iteration 自然數(shù)1開(kāi)始序號(hào)
@index 0開(kāi)始序號(hào)
@first 如果第一個(gè)項(xiàng)目則返回1,否則返回false
@last 如果最后一個(gè)項(xiàng)目則返回1,否則返回false
@show 判斷數(shù)組是否為空
@total 數(shù)組的元素個(gè)數(shù)
二維數(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=”名稱(chēng)” loop=$animal start=2 step=2 max=5 show=false}{$animal[名稱(chēng)]}關(guān)鍵字: {$smarty.section.名稱(chēng).first}{$smarty.section.名稱(chēng).last}{$smarty.section.名稱(chēng).iteration}{$smarty.section.名稱(chēng).total} {/section }foreach和section最大的區(qū)別是:
foreach可以遍歷索引和關(guān)聯(lián)數(shù)組
section只給遍歷索引數(shù)組
for循環(huán)
<body><h2>for循環(huán)語(yǔ)句</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)語(yǔ)句</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'=>'請(qǐng)選擇','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">請(qǐng)選擇</option>{html_options options=$val_out1}</select></div></body>總結(jié)
以上是生活随笔為你收集整理的Smarty模板技术学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【每日SQL打卡】
- 下一篇: Material Design入门(二)