PHP之Smarty
生活随笔
收集整理的這篇文章主要介紹了
PHP之Smarty
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PHP之Smarty
Smarty簡(jiǎn)介
smarty的引入
1. 為了分工合作,模板頁(yè)面中最好不要出現(xiàn)php代碼 2.需要將表現(xiàn)和內(nèi)容相互分離 3.通過(guò)smarty把php和html頁(yè)面顯示在頁(yè)面上官方smarty
plugins:自定義插件 sysplugins:系統(tǒng)插件 Smarty.class.php:Smarty核心文件smarty的屬性和方法
class Smarty{public $left_delimiter="{";//左界定public $right_delimiter="}";//右界定protected $template_dir=array('./templates/');//默認(rèn)模板文件目錄protected $compile_dir='./templates_c';//默認(rèn)混編目錄protected $config_dir=array('./configs/');//默認(rèn)配置目錄protected $cache_dir='./cache/';//默認(rèn)緩存目錄public function setTemplateDir(){}//設(shè)置模板文件夾public function setConfigDir(){}//設(shè)置配置文件夾public function setCompileDir(){}//設(shè)置混編文件夾public function setCacheDir(){}//設(shè)置緩存文件夾 }smarty簡(jiǎn)單的操作
1. 將libs目錄拷貝到站點(diǎn)下,改名為smarty 2. 創(chuàng)建模板目錄templates 3. 創(chuàng)建混編目錄templates_c 4. 在站點(diǎn)下創(chuàng)建1-demo.php 5. 在模板下創(chuàng)建1-demo.html require './smarty/Smarty.class.php'; $smarty = new Smarty(); $smarty->assign('title','鋤禾'); $smarty->display('1-demo.html'); <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> {$title} </body> </html>smarty的注釋
語(yǔ)法:{* *} 注意:smarty注釋在源碼中看不見(jiàn) smarty的注釋只要在左右定界符里面加上*就可以了smarty變量
smarty中變量有3種,普通變量、配置變量、保留變量普通變量
普通變量就是我們自定義的變量方法一:在PHP中定義
$smarty->assign('name','tom');//給變量賦值方法二:可以在模板中定義
語(yǔ)法:{assign var='變量名' value='值'} 例如:{assign var='sex' value='男'} 簡(jiǎn)化寫(xiě)法:{$add='北京'}保留變量
smarty中有一個(gè)特殊的保留變量(內(nèi)置變量),類似于PHP中所有的超全局變量、常量、時(shí)間等信息| {$smarty.get.name} | 獲取get提交的name的值 |
| {$smarty.post.name} | 獲取post提交的name的值 |
| {$smarty.request.name} | 獲取get和post提交的name的值 |
| {$smarty.cookies.name} | 獲取cookie中name的值 |
| {$smarty.session.name} | 獲取session中name的值 |
| {$smarty.const.name} | 獲取常量name |
| {$smarty.server.DOCUMENT_ROOT} | 獲取服務(wù)器的虛擬目錄地址 |
| {$smarty.config.name} | 獲取配置文件中的值 |
| {$smarty.now} | 時(shí)間戳 |
| {$smarty.ldelim} | 獲取左界定 |
| {$smarty.rdelim} | 獲取右界定 |
配置變量
從配置文件中獲取變量值,配置文件默認(rèn)的文件夾是configs 1. 在站點(diǎn)下創(chuàng)建配置文件夾configs 2. 在configs目錄下創(chuàng)建smarty.conf文件 color=#FF0000 size=50px [spring] # 配置文件中的段落也稱作節(jié) color=#009900 size=20px [winter] color=#000005 size=5pxHTML頁(yè)面
<!DOCTYPE html> <html lang="en"> <!--config_loads默認(rèn)目錄就是當(dāng)前目錄的configs目錄 它會(huì)自動(dòng)找當(dāng)前目錄的configs,所以不需要寫(xiě)./configs/的文件--> <!--這里section引入了節(jié)--> {config_load file='smarty.conf' section=''}<head><meta charset="UTF-8"><title>Title</title><style>body{color: {#color#};font-size:{$smarty.config.size};}</style> </head> <body> {if $smarty.get.score gt 90}A {elseif $smarty.get.score gte 80}B {else}C {/if} 鋤禾日當(dāng)午 </body> </html>PHP頁(yè)面
require './smarty/Smarty.class.php'; $smarty = new Smarty(); $smarty->assign('title','鋤禾'); $smarty->display('1-demo.html'); 小結(jié): 1. 要使用配置文件中的值,首先必須引入配置文件,通過(guò){config_load}標(biāo)簽引入 2. 獲取配置文件中的值的方法有兩種 (1):{#變量名#} (2):{$smarty.config.變量名} 配置文件中的注意事項(xiàng): 1. 全局的一定要寫(xiě)在節(jié)的前面 2. 配置文件中[]表示節(jié) 3. 配置文件中的注釋是#smarty運(yùn)算符
| eq | 相等 |
| neq | 不等于 |
| gt | 大于 |
| lt | 小于 |
| lte | 小于等于 |
| gte | 大于等于 |
| is even | 是偶數(shù) |
| is odd | 是奇數(shù) |
| is not even | 不是偶數(shù) |
| is not odd | 不是奇數(shù) |
| not | 非 |
| mod | 求模取余 |
| div by | 被整除 |
| is [not] div by | 是否被某數(shù)整除 |
判斷
語(yǔ)法: {if 條件} {elseif 條件} {else} {/if}數(shù)組
smarty中訪問(wèn)數(shù)組的方式有兩種: 1. 數(shù)組[下標(biāo)] 2. 數(shù)組.下標(biāo)PHP頁(yè)面
require './smarty/Smarty.class.php'; $smarty = new Smarty(); $stu = ['tom','berry'];//索引數(shù)組 $stu1 = array('name'=>'zhangsan','age'=>22); $goods = array(array('name'=>'手機(jī)','price'=>3352),array('name'=>'鋼筆','price'=>10) ); $smarty->assign('stu',$stu); $smarty->assign('stu1',$stu1); $smarty->assign('goods',$goods); $smarty->display('2-demo.html');HTML頁(yè)面
<html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> 學(xué)生:{$stu[0]}-{$stu.1}<br> 學(xué)生1:{$stu1['name']}的年齡是{$stu1.age}<br> 商品: <ul><li>{$goods[0]['name']}</li><li>{$goods.0.price}</li><li>{$goods.1['name']}</li><li>{$goods[1].price}</li> </ul> {for $i=1 to 5}{$i}:鋤禾日當(dāng)午<br> {/for} <hr> {for $i=1 to 5 step=2} {$i}:鋤禾日當(dāng)午<br> {/for} </body> </html>循環(huán)
smarty中支持的循環(huán)有:{for}、{while}、{foreach}、{section}。用的最多的是foreach循環(huán) 語(yǔ)法: {foreach 數(shù)組 as $k=$v} {foreachelse}沒(méi)有輸出 {/foreach} foreach的屬性: @index:從0開(kāi)始的索引 @iteration:從1開(kāi)始的編號(hào) @first:是否是第一個(gè)元素 @last:是否是最后一個(gè)元素 {for $i to 5} {$i}:鋤禾日當(dāng)午<br> {/for} <hr> {for $i to 5 step=0} 步長(zhǎng)為2的時(shí)候{$i}:鋤禾日當(dāng)午<br> {/for} for語(yǔ)法: {for 初始值 to 結(jié)果值 [step 步長(zhǎng)]} {/for} while語(yǔ)法: {while 條件} {/while} 注意:while條件有的和php類似自定義變量修飾符
變量修飾符存放在plugins目錄中 規(guī)則: 1. 文件的命名規(guī)則:modifier.變量修飾器名稱.php 2. 文件內(nèi)方法命名規(guī)則:smarty_modifier_變量修飾器名稱(形參...){}避免smarty解析
smarty的定界符和css、js、中的大括號(hào)產(chǎn)生沖突的時(shí)候,css、js中的大括號(hào)不要被smarty解析 1. 更換定界符 2. 左大括號(hào)后面添加空白符 3. 用{literal}{/literal}來(lái)包含js和CSS緩存
頁(yè)面緩存,空間緩存,數(shù)據(jù)緩存,smarty中的緩存就是頁(yè)面緩存 開(kāi)啟緩存:$smarty->caching=true或者1 //開(kāi)啟緩存 緩存的更新: 1. 刪除緩存,系統(tǒng)會(huì)重新生成新的緩存文件 2. 更新模板文件,配置文件,緩存自動(dòng)更新 3. 過(guò)了緩存的生命周期,默認(rèn)是3600秒 4. 強(qiáng)制更新:$smarty->force=true; 5. 緩存的生命周期:$smarty->cache_lifetime=-1|0|N -1:永遠(yuǎn)不過(guò)期 0:立即過(guò)期 N:有效期是N秒,默認(rèn)是3600秒局部不緩存:
1. 變量不緩存 {$變量名 nocache} 2. 整個(gè)塊不緩存:{nocache} {/nocache}例子:
不緩存:{$smarty.now nocache} 不緩存:{nocache} {$smarty.now}<br> {/nocache}緩存分頁(yè)
$smarty->caching=1; $smarty->display('模板頁(yè)面','識(shí)別id');緩存集合
$smarty->display('模板頁(yè)面','$color|$size');清楚緩存
$smarty->clearCache('模板',[識(shí)別id]); $smarty->clearAllCache();//清楚所有緩存總結(jié)
以上是生活随笔為你收集整理的PHP之Smarty的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 群晖NAS教程(二十三)、利用Docke
- 下一篇: php smarty extends,p