Smarty模板引擎技術二,smarty模板引擎_PHP教程
Jul 12, 2016 am 08:55 AMSmarty模板引擎技術二,smarty模板引擎
Smarty模板引擎技術
作用:載入一個php文件,將載入的文件的內(nèi)容賦值給一個變量
?
注意:該內(nèi)建函數(shù)只能在2.0中使用,如果使用的話,必須得實例化SmartyBC.class.php
?
示例代碼:
Index.php
include 'Smarty/SmartyBC.class.php';
//實例化Smarty類
$Smarty = new SmartyBC();
$Smarty->assign('name','小明');
$Smarty->display('index.tpl');
index.tpl
<code><span><strong><em><span>{*<span>include_php</span></span>內(nèi)建函數(shù)<span>*}<br/></span></em><span>{<span>include_php <span>file=<span>"date.php" <span>assign=<span>"date"<span>}<br/>{<span>$<span>date}</span></span></span></span></span></span></span></span></span></strong> </span></code>
?
作用:當Smarty內(nèi)置的功能不夠使用時,可以通過insert內(nèi)建函數(shù)拓展功能。
?
基本語法:index.php文件中創(chuàng)建一個
insert_自定義函數(shù)名($arg){
????????echo $arg[模板中定義的變量]????
}
Index.tpl文件
{insert name=自定義函數(shù)名稱 自定義參數(shù)… }
示例代碼:
Index.php
function insert_func($arg){
echo $arg['title'];
}
function insert_date($arg){
echo $arg['say']."今天天氣好棒的說,現(xiàn)在的時間是".date('Y-m-d H:i:s',time());
echo '
';
echo $arg['zhangsan']."今天天氣好棒的說,現(xiàn)在的時間是".date('Y-m-d H:i:s',time());
}
index.tpl模板
{insert name="func" title='PHP是世界上最好的語言'}
<hr>
{insert name="date" say='老王說:' zhangsan='哈哈哈:'}
?
?
?
?
?
示例代碼:
{if $age >= 18}
此人非未成年
{elseif $age == '14'}
此人14歲
{else}
此人是未成年
{/if}
?
?
示例代碼:
<b>在Smarty中我們使用{ldelim}if{rdelim}進行條件判斷b>
作用:在該標簽中的任何內(nèi)容都不會受Smarty模板引擎解析
示例代碼:
{literal}
{*嗨,我是一個注釋*}
{assign var='age' value="14"}
{/literal}
運行效果:
?
作用:在該標簽中可以使用原生的PHP代碼。
示例代碼:
{*PHP內(nèi)建函數(shù)*}
{php}
echo date('Y-m-d h:i:s',time());
echo '<br>';
echo "我在php內(nèi)建函數(shù)中的內(nèi)容";
{/php}
運行效果:
?
7、strip內(nèi)建函數(shù)
作用:去除空格符和換行符
示例代碼:
{strip}
<table>
<tr>
<td>1td>
<td>2td>
<td>3td>
<td>4td>
<td>5td>
tr>
table>
{/strip}
使用前:
?
?
?
?
?
?
使用后:
8、section、sectionelse內(nèi)建函數(shù)(二維數(shù)組,)
作用:遍歷數(shù)組
基本用法:
{section loop=$arr name="index"}
{$arr[index]}
<br>
{/section}
參數(shù)詳解:loop 要遍歷數(shù)組
???????? Name 當前循環(huán)的索引
拓展使用1:使用start step max
{section loop=$arr1 name="index" start="0" step="1" max="5"}
{$arr1[index]}
<br>
{/section}
- 參數(shù)詳解:start????循環(huán)的起始索引
???????? Step????每次循環(huán)增加的數(shù)量
???????? Max 最大的循環(huán)次數(shù)
拓展使用2:sectionelse
作用:判斷循環(huán)的數(shù)組是否為空,如果為空的話,則執(zhí)行后面的內(nèi)容。
實例代碼:
{*section內(nèi)建函數(shù)*}
{section loop=$arr1 name="index" start="0" step="1" max="5"}
{$arr1[index]}
<br>
{sectionelse}
<b>):沒有數(shù)組或者數(shù)組為空b>
{/section}
- 遍歷二維數(shù)組
實例代碼:
Index.php
$arr3 = array(array('name'=>'小明','age'=>25,'sex'=>'未知'),
array('name'=>'老王','age'=>26,'sex'=>'男'),
array('name'=>'老李','age'=>27,'sex'=>'你猜')
);
Index.tpl
{section loop=$arr3 name="index" }
{$arr3[index]['name']} |
{$arr3[index]['age']} |
{$arr3[index]['sex']} |
<hr/>
{/section}
運行效果:
?
拓展內(nèi)容:
?
<code><span><strong>當前索引: <span><span>{<span>$<span>smarty.section.index.index} <span>->><br/></span></span></span></span>當前索引的前一個:<span>{<span>$<span>smarty.section.index.index_prev}<span>->><br/></span></span></span></span>當前索引的下一個:<span>{<span>$<span>smarty.section.index.index_next}<span>->><br/></span></span></span></span>當前所循環(huán)的次數(shù)<span> <span><span>{<span>$<span>smarty.section.index.iteration}<span>->><br/></span></span></span></span>判斷當前是否第一次<span><span>: <span>{<span>$<span>smarty.section.index.first}<span>->><br/></span></span></span></span></span>判斷當前是否最后一次:<span>{<span>$<span>smarty.section.index.last}<span>->> </span></span></span></span></span></span></span></span></strong></span></code>
循環(huán)的總次數(shù):{$smarty.section.index.total}->>
?
運行效果:
示例代碼:
{counter start='10' }
<hr>
{counter}
<hr>
{counter print=false}
<hr>
{counter skip=2}
<hr>
{counter}
<hr>
運行效果:
基本語法:{cycle values=參數(shù)1,參數(shù)2,參數(shù)3}
?
示例代碼:
{*cycle實現(xiàn)各行換色*}
<hr>
<table width="100%">
<tr style="background-color: {cycle values="red,green,yellow"}">
<td>我的第{counter start='1' skip='1'}次循環(huán)td>
tr>
<tr style="background-color: {cycle values="red,green,yellow"}">
<td>我的第{counter}次循環(huán)td>
tr>
<tr style="background-color: {cycle values="red,green,yellow"}">
<td>我的第{counter}次循環(huán)td>
tr>
<tr style="background-color: {cycle values="red,green,yellow"}">
<td>我的第{counter}次循環(huán)td>
tr>
<tr style="background-color: {cycle values="red,green,yellow"}">
<td>我的第{counter}次循環(huán)td>
tr>
<tr style="background-color: {cycle values="red,green,yellow"}">
<td>我的第{counter}次循環(huán)td>
tr>
table>
?
{debug}
運行效果:
作用:捕獲一個文件的內(nèi)容,然后賦值給一個變量
示例代碼:
{fetch file="shi.txt" assign="text"}
{fetch file="date.php" assign="php"}
{*變量調節(jié)器
{$text|變量調節(jié)器名稱:參數(shù)1:參數(shù)2}
*}
{$php}
<hr>
{$text|nl2br}
運行效果:
<code><span><strong>{<span>html_image <span>file=<span>"1.jpg"<span>}</span></span></span></span></strong> </span></code>
?
參數(shù):file????圖片資源的路徑
運行效果:
?
作用:生成一個表格,將數(shù)據(jù)遍歷進去
示例代碼:
{html_table loop=$arr cols="3"}
參數(shù)說明:loop????要循環(huán)遍歷的數(shù)組
???? Cols????指定表格列數(shù)
作用:生成一組多選框
示例代碼:
{html_checkboxes name = 'job'
values = $arr
checked = $arr2
output = $arr3
separator = "|"
}
參數(shù)說明: name????????對應多選框中的name屬性
???????? Values????對應多選框中你的value屬性
Checked????選中指定的多選框
???????? Output????控制文本內(nèi)容
???????? Separator 連接符
運行效果:
{待補充}
示例代碼:
<code><span><strong><<span>select <span>style=<span>"<span>width: <span>100<span>%;<span>"<span>><br/> <span>{<span>html_options <span>values = <span>$<span>arr<br/> selected = <span>'GO'<br/> <span>output = <span>$<span>arr3<br/> }<br/><span></<span>select<span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></strong><span> </span></span></code>
參數(shù)說明:values???? ????下拉框選項的值
???? Selected????指定被選中的下拉選項,注意:必須和value的值對應
???????? Output????輸出的文本
9、html_radios自定義函數(shù)
示例代碼:
<code><span><strong>{<span>html_radios <span>values = <span>$<span>arr<br/> checked = <span>'nodejs'<br/> <span>output = <span>$<span>arr3<br/> separator = <span>"|"<br/><span>}</span></span></span></span></span></span></span></span></span></span></strong> </span></code>
?
參數(shù)說明:values????單選框的值
???????? Checked????指定默認被選中的單選框
???????? Output????輸出的文本
???????? Separator????連接符
運行效果:
{待補充}
實例代碼:
{section loop=$arr4 name="index" }
{html_image file="./img/{$arr4[index]}.jpg"}
<hr>
{/section}
?
SMARTY_DIR
- $template_dir????????模板目錄????默認是:templates
- $compile_dir????????編譯目錄????默認是:templates_c
- $config_dir????????配置目錄????默認:configs
- $cache_dir????????緩存目錄????默認:cache
- $left_delimiter????左定界符????默認:{
- $right_delimiter????右定界符????默認:}
?
以上變量都有默認行為。
- $caching????????????是否開啟緩存
- $cache_lifetime????緩存的生命周期:默認3600s
- $debugging????????開啟調試模板
- $php_handling????是否允許在模板中引入php
3、常用方法
- assign 向模板中傳遞變量
- assignByRef????分配變量到模板文件(按引用傳遞)
- append???????? 追加變量
$Smarty->append('var1','小明');
$Smarty->append('var1','25');
$Smarty->append('var1','男');
- appendByRef追加不同的數(shù)據(jù)到模板的數(shù)組變量中(按引用傳遞)
- clearAllAssign???? 清除模板中所有變量
$Smarty->clearAllAssign(); //清除所有模板變量
- clearAssign 清除指定的變量
$Smarty->clearAssign('title');
- clearCache???? 清除緩存
- configLoad???? 配置加載
$Smarty->configLoad('config.conf','class2');
- clearConfig????清除配置內(nèi)容
$Smarty->clearConfig('name');
- display????????指定渲染模板
$Smarty->display('index_3.tpl');
加載模板文件
渲染模板
顯示模板
- fetch????????捕獲模板但是不輸出
?
加載模板文件
渲染(將標簽替換為php代碼)模板文件
我們可以通過fetch實現(xiàn)靜態(tài)技術。
Index_3.php文件
/*
* 如果有靜態(tài)文件則加載靜態(tài)文件,如果沒有靜態(tài)生成一個靜態(tài)文件。
* */
if(!is_file('./html/index_3.html')){
$html = $Smarty->fetch('index_3.tpl');
file_put_contents('./html/index_3.html',$html);
echo '這里是沒有靜態(tài)文件';
include "./html/index_3.html";
}else{
echo '這里是有靜態(tài)文件';
include "./html/index_3.html";
}
[待補充]
- templateExists 判斷模板文件是否存在????
if($Smarty->templateExists('index_4.tpl')){
$Smarty->display('index_3.tpl');
}else{
echo '矮油,模板文件不在哦~';
}
?
?
補充:如何在smarty模板中讀取數(shù)組,對象。
?
- 在模板中獲取數(shù)組內(nèi)容:
{*多維數(shù)組*}
{$var[0]['name']}
{*一位數(shù)組*}
{$var['name']}
- 在模板中獲取對象內(nèi)容
{*獲取對象屬性*}
{$std->name}
<hr>
{*獲取對方法*}
{$person->speak()}
Index3.php文件
$std
= new StdClass();
class person{
function __construct(){}
function speak(){
echo '你猜猜我誰?';
}
}
$person = new person();
$std->name = '張二明';
?
?
概念:一般在我們項目中,有一部分數(shù)據(jù)并不是實時更新的,但是,有又必須實時訪問。如果不使用緩存技術的話,每訪問一次,得查詢一次或者多次數(shù)據(jù)庫,那么會給數(shù)據(jù)造成很高的I/O開銷。會增加服務器的壓力。
用戶端緩存原理:
?
服務器緩存原理:
?
//開啟緩存
$Smarty->caching = true;
//設置緩存文件的生命周期
$Smarty->cache_lifetime = '7200';
緩存文件由編譯文件而來。
?
編譯文的內(nèi)容何時變化?
?
思路:設置一個生命周期只有30秒的緩存文件,定義一個變量,在模板中使用該變量。然后打開index.php.
然后修改該變量的值。等待。。。30
Smarty緩存相關細節(jié)
<code><span><strong>$Smarty<span>->isCached(<span>'index_4.tpl'<span>)</span></span></span></strong> </span></code>
- 基本語法:isCached(templatesName); 檢測模板文件的緩村文件是否存在
- 參數(shù)說明:模板文件名稱
<code><span><span><strong><em>//</em></strong></span><span><strong><em>清除所有緩存<br/><span><span>$<span>Smarty->clearAllCache();<br/><span>//</span></span></span><span>清楚某模板緩存文件<br/></span></span></em><span>$Smarty<span>->clearCache(<span>'index_4.tpl'<span>);</span></span></span></span></strong> </span></span></code>
?
在Smarty的緩村是全局緩存,如果開啟緩存,訪問整個頁面的數(shù)據(jù)都會被緩存,如果頁面中有一些動態(tài)數(shù)據(jù)需要修改,如何處理?
如何處理頁面中動態(tài)顯示的數(shù)據(jù)部分呢?
<code><span><strong><span>$Smarty<span>->assign(<span>'shige'<span>,<span>'</span></span></span></span></span>《再別康橋》<span>'<span>,<span>true<span>);</span></span></span></span></strong> </span></code>
<code><span><strong>{<span>nocache<span>}<br/> {<span>$<span>title}<br/>{/<span>nocache<span>}</span></span></span></span></span></span></strong> </span></code>
如何解決一個模板文件,動態(tài)顯示不同的內(nèi)容。
實際場景:譬如一個電商網(wǎng)站的商品詳細頁,會動態(tài)的根據(jù)URL上的參數(shù),改變該頁面的內(nèi)容。
那么這種情況如何實現(xiàn)緩存呢?
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=1
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=2
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=3
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=250
?
在Smarty中,我們通過設置display()第二個參數(shù),來實現(xiàn),單頁面,多緩存。
11、緩存集合
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=1&cate_id=15
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=1&cate_id=15
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=1&cate_id=15
http://localhost:63354/Smarty/Smarty02/index5.php?goods_id=1&cate_id=15
我們通過給display()方法設置第二個參數(shù)實現(xiàn)緩存集合(通過|分割)
<code><span><span><strong>$Smarty</strong></span><span><strong>->display(<span>'index_5.tpl'<span>,<span>$_GET<span>[<span>'goods_id'<span>].<span>'|'<span>.<span>$_GET<span>[<span>'cate_id'<span>]);</span></span></span></span></span></span></span></span></span></span></span></span></strong> </span></span></code>
?
五、過濾器
示例代碼:
<code><span><strong><span>//</span><span>定義一個函數(shù)用于字符串替換</span><span> </span></strong></span></code>
<code><span><strong><span>function <span>check<span>(<span>$tpl_output<span>, <span>$smarty<span>){<br/> <span>$tpl_output <span>= <em>str_replace</em>(<span>'</span></span></span></span></span></span></span></span></span></span>蒼井空<span><span>'<span>,<span>'</span></span></span>張某某<span>'<span>,<span>$tpl_output<span>);<br/> <span>return <span>$tpl_output<span>;<br/>} </span></span></span></span></span></span></span></span></strong></span></code>
<code><span><strong><span>//</span><span>通過注冊過濾器,實現(xiàn)模板中的字符串過濾</span></strong><span><strong><br/><span>$Smarty<span>->registerFilter(<span>"output"<span>,<span>"check"<span>);</span></span></span></span></span></span></strong> </span></span></code>
?
通過模板繼承實現(xiàn)頁面精細化拆分
示例代碼:
Parent.tpl文件
<code><span><span><strong>{<span>extends <span>file=<span>"parent.tpl"<span>}<br/>{<span>block <span>name=<span>'content'<span>}<br/> </span></span></span></span></span></span></span></span></strong></span><span><strong>偶哈呦,哈哈哈哈<br/> <span><<span>hr<span>><br/><span>{/<span>block<span>}</span></span></span></span></span></span></strong> </span></span></code>
Child.tpl文件
<code><span><span><strong>{<span>extends <span>file=<span>"parent.tpl"<span>}<br/>{<span>block <span>name=<span>'content'<span>}<br/> </span></span></span></span></span></span></span></span></strong></span><span><strong>偶哈呦,哈哈哈哈<br/> <span><<span>hr<span>><br/><span>{/<span>block<span>}</span></span></span></span></span></span></strong> </span></span></code>
?

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

??? ??











? ????? ?? ???? ??? ???(?: ?? ? ???)?? ??? ???? ???? ??, ?? ??(PV) ???? ??(BEV) ???? ??? ????? ???? ??? ?????. VT(Visual Transformation) ??? ?? ?????. ?? ??? ?? 2D?? 3D?, 3D?? 2D? ???? ? ?? ???? ????. 2D?? 3D?? ??? ?? ??? ???? ??? 2D ??? ?????, ?? ? ????? ?? ??? ???? ?????? ?? ????? ??? ? ????. 3D?? 2D?? ??? ????? 3D ??? ???? 2D ??? ????? Transformer? ?? 3D? 2D ?? ?? ??? ?? ?? ???? ????? ?? ? ?? ??? ?????.

nohup? ??? ?? ?? nohup? ??? ? ??? ?? ?????? ???? ?? ??? ????? ??? ?? ??? ??????? ??? ???? ? ????? ???? ?????. ??? ?? ???? ??. ?? ???? nohup ??? ??? ??? ??? ????????. 1. nohup? ??: ??????? ?? ??: nohup ??? ?? ???? ??? ??? ???? ??? ?? ?? ?? ?? ??? ??????? ?? ????? ? ? ????. ?? ???? ?

9? 23?, ?????????, JD.com ? ??? ????? "DeepModelFusion:ASurvey"?? ??? ??????. ? ?? ??/??? ?? ? ?? ??? ????? ??? ?? ??? ???? ??? ?????. ?? ? ?? ??? ?? ?? ??? ??? ??? ???? ?? ??? ??? ??? ?????. ??? ? ?? ??(?: LLM ? ?? ??)? ?? ? ?? ??? ?? ?? ??, ??? ???? ??, ?? ?? ?? ?? ?? ?? ?? ??? ? ?? ??? ?????. ? ????? ?? ?? ?? ?? ??? ? ?? ??? ????. (1) ? ?? ?? ?? ??? ?? ?? ?? ?? ??? ?? ??? ??? ???? ???? "?? ??"

PHP?? ?? ??? ???? ?? 1. ?? ??? ?? ?? ?? PHP?? ?? ??? ??? ???? ? ???? ?? ?????. ??? ???? ?? ?? ?? ???? ????. ?? ??? ???? ??? ??? ???? ???? ???? ? ??? ??????. 2. ?? ??? ?? ?? ??? ?? ??? ??? ????: Define("?? ??","?? ?&qu

?? ??? & ??? ???? ??? ??? ?? 3D ???? ?? ??? ???? ??? ??? 3D ??? ???? ??? ????? ????. ?? ?? ??? 3?? ??? ?? ??? ? ?? ???? ??? ?????. ? ?? ??? ???, ??? ?? ? ??? ??? ??? 3D ??? ??? ??? ?? ????. ?? ??, ?? ??, ?? ?? ? ?? ??? ???? ??? ???? ??? ?? ??? ?? ??? ?????. ???? ?? ??? ???? ??? ???? ????. ?? ??? ??? ??? 3D ??? ??? ??? ? ?? ??? ??? ??? ? ????? ??? ???? ?? ??? ????. ??? ? ??? ??? ??? ?? ??? ?? ???? ??? ?????. (?????? ?? ???????.

???? ????? ?? ???? Go ??? JavaScript? ??? ??? ?????? ?? ?????. ???? ??? ???? ???? ??? Go ??? ??? ??? ? ??? ?? ?????? ??? ?? JavaScript? ?? ????? ?? ? ?? ?? ???? ???? ???? ??? ???? ???? ?????. ??? ?? ?? ???? ? ???? ??? ??? ??? ? ??? ??? ??? ????? ???? ???? ?????. ?????? ???, ????? ??? ?? ??? ??

PPT ???? ???? ?? ???? ??? ?? ???. ???? ???? PPT? ?? ? ? ???? ??? ?? ??? ?? ?? ???? ??? ??? ?? ??? PPT ???? ????? ??? ????? ??? ??? ????. ? ???? ?? ??? ???? ???, ??? ? ???? ?? ? ???? ??????. ??? ?? ???, PPT ???? ? ? ???? ??? ???. ???? PPT ???? ???? ??? ?????? ??? ?????. 1. ?? PPT? ?? ? ??? ??? ?? [?? ?? ??]? ??? ??? ???? ???? ??? ?????. 2. [??] ??, ???? ?? ?? 3. [??] ??, [??] ??

Golang? ????? ??? ??: Golang? ????? ???? ?? ??? ??? ????? ???? ?? ??? ?????. ???? ??? ??????? ??? ???? ?? ????? ??? ?? ? ????? ????. ? ????? ??? ??? ????? ??? Golang? ??? ??? ? ? ????. ? ????? Golang? ????? ??? ??? ????? ???? ?? ?? ??? ?? ????? ????? ???? ?????. ????? ???? Golang? ??? ????? ???? ??? ?? ????.
