Smarty模板技术学习
模板引擎技術:使得php代碼和html代碼分離的技術就稱為“模板引擎技術”
自定義smarty模板技術實現
<?php//迷你smarty原理 class MiniSmarty{public $var_arr = array();public $template_dir = "./view/";public $compile_dir = "./view_c/";//把外部聲明變量設置為當前類內部的成員屬性信息var_arrfunction assign($k,$v){$this ->var_arr[$k] = $v;}//顯示模板內容function display($tpl){include $this->compile($tpl);}//把html模板內容引入,替換{ ---------》< ?php echo// } ---------》 ; ? >//模板編譯,把{}編譯為php標簽內容//$tpl: 被編譯模板文件的名稱function compile($tpl){//$tpl = "order.html";$tpl_file = $this -> template_dir.$tpl; //模板文件$com_file = $this->compile_dir.$tpl.".php"; //編譯文件//判斷編譯文件是否存在,如果存在直接調用,并且該編譯文件生成后對應的模板文件沒有再修改//正常情況,編譯文件的時間稍大,模板文件時間相對小一些if(file_exists($com_file) && (filemtime($tpl_file)<filemtime($com_file))){return $com_file;exit;}//把$tpl內容抓取出來,替換內部的{}內容$cont = file_get_contents($tpl_file);//替換{ ---------》< ?php echo//$cont = str_replace("{","< ?php echo ",$cont);//在模板中調用的變量即是 當前對象的屬性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);//把生成好的內容放入一個文件里邊,然后引入之file_put_contents($com_file, $cont);//引入$com_file編譯文件return $com_file;} }通過MySmarty對Smarty進行初始化設置
<?php//在這個類里邊設置smarty公共配置 include "./libs/Smarty.class.php";class MySmarty extends Smarty{function __construct(){parent::__construct(); //先執行父類的構造函數,避免被覆蓋//更改smarty的左右標記$this -> left_delimiter = "{";$this -> right_delimiter = "}";$this -> setTemplateDir('.' . DS . 'view' . DS);//修改模板目錄$this -> setCompileDir('.' . DS . 'view_c' . DS);//修改編譯文件目錄} }smarty的三種變量使用
1.smarty?>assign(名稱,值);2.超級全局數組變量使用_GET POST_SESSION COOKIE_ENV $_SERVER等等
<body>當前用戶:{$smarty.session.username}<br />名字:{$name}<br />顏色:{$color}<br />地區:{$smarty.get.addr}<br />年齡:{$smarty.get.age}<br />{$smarty.now}{*當前時間戳信息*}<br />{$smarty.const.HOST}{*獲得常量信息*}<br />{$smarty.template}{*當前請求的模板*}<br />{$smarty.current_dir}{*當前模板路徑*}<br />{$smarty.version}<br />{$smarty.ldelim},{$smarty.rdelim} {*左右標記信息*}<br /></body>3.配置變量信息
通過配置變量的定義 和 段 的設置可以輕松實現同一頁面根據用戶不同喜好顯示不同的樣式。
定義
[shop] POLICE=京公網安備110000000011號 NETWORK=互聯網出版許可證 BROADCAST="廣播電視節目制作經營許可證 (京)字第434號"[car] POLICE=京公網安備973498378號 NETWORK=互聯網出版許可證02 BROADCAST="廣播電視節目制作經營許可證 (京)字第978號"通過以下語句引入配置
{config_load file=”site.conf” section=”car”}
{}標記沖突的問題
1.把Smarty的標記變為其他標記
2.把{}符號變為不同行
3.利用literal標簽把有{}的內容給括起來
數組/對象變量的使用
數組:數組[下標] 或 數組.下標
對象:對象->成員
訪問
<body><h2>訪問數組元素信息(索引)</h2><div>{$fruit[2]}<br />{$fruit[3]}<br />{$fruit.0}<br />{$fruit.1}<br /></div><h2>訪問數組元素信息(關聯)</h2><div>{$animal['helan']}<br />{$animal.north}<br /></div><h2>訪問數組元素信息(二維)</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>遍歷數組信息foreach/section
{foreach 數組 as k=>v}
@iteration 自然數1開始序號
@index 0開始序號
@first 如果第一個項目則返回1,否則返回false
@last 如果最后一個項目則返回1,否則返回false
@show 判斷數組是否為空
@total 數組的元素個數
二維數組遍歷
<body><h2>二維數組遍歷</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[名稱]}關鍵字: {$smarty.section.名稱.first}{$smarty.section.名稱.last}{$smarty.section.名稱.iteration}{$smarty.section.名稱.total} {/section }foreach和section最大的區別是:
foreach可以遍歷索引和關聯數組
section只給遍歷索引數組
for循環
<body><h2>for循環語句</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分支結構語句</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>復選框、單選按鈕、下拉列表
<?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>復選框設置</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'=>'小學','B'=>'初中','C'=>'高中','D'=>'大學')); $smarty -> assign('sel',array('A','D'));$smarty -> assign('val_out1',array('man','girl','secret'));$smarty -> display('14.html'); <body><h2>下拉列表設置</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>總結
以上是生活随笔為你收集整理的Smarty模板技术学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【每日SQL打卡】
- 下一篇: Material Design入门(二)