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