常用視圖引擎的介紹
視圖引擎的作用
幫助mvc中view更方便的開發(fā)
好的視圖引擎
- 基于該引擎開發(fā)出的模板更貼近標準的html
- 語法簡單易懂
- 良好的緩存機制
- 拓展性良好
- 網(wǎng)絡(luò)資源多
常見視圖引擎
smarty phplib
什么是smarty
php模板引擎,實現(xiàn)代碼分離
smarty的引入、配置與實例制作
smarty的引入與實例化
smarty配置
$smarty->left_delimiter = '{';//左定界符
$smarty->right_delimiter = '}';//右定界符
$smarty->template_dir = 'tpl';//html模板的地址
$smarty->compile_dir = 'template_c';//模板編譯生成的文件
$smarty->cache_dir = 'cache';//緩存
以下是開啟緩存的另外兩個配置。因為通常不用smarty的緩存機制,所以只做了解
$smarty->caching = true;//開啟緩存
$smarty->cache_lifetime = 120;//緩存時間
創(chuàng)建目錄 /template_c /cache
兩個常用的方法
$smarty->assign('key','value');
模板變量賦值
$smarty->display('tpl');
展示模板
變量調(diào)節(jié)器
- 首字母大寫
{$key|capitalize}
- 字符串連接
{$key|cat:"xxx"}
- 日期格式化
{$key|date_format}
- 為未賦值或為空的變量指定默認值
{$key|default:'this is null'}
- 轉(zhuǎn)碼 escape 用于html轉(zhuǎn)碼,在沒有轉(zhuǎn)碼的變量上轉(zhuǎn)換單引號,十六進制轉(zhuǎn)碼,十六進制美化,或者javascript轉(zhuǎn)碼,默認是html轉(zhuǎn)碼
{$key|escape:"url"}
- 大小寫
{$key|upper}
{$key|lower}
其他函數(shù),參見smarty手冊
條件判斷
-
eq、=、gt、>、lt、<、neq、!=
{if $key eq 1}
{elseif $name eq 2}
{else}
{/if}
- 循環(huán)
{foreach from=$key item=vo}
{foreachelse}
{/foreach}
- 引入
{include file="路徑" }
函數(shù)
{"Y-m-d"|date:$key}
{'d'|str_replace:"xxx":$key}