Smarty s02
保留變量? 方便使用php
?????????????????? 不用assign
?????????????????? {$smarty}
?????????????????? get
??????????????????????????? {$smarty.get.page}
?????????????????? session
??????????????????????????? {$smarty.session.user.name}
?????????????????? server
?????????????????? cookies
?????????????????? request
?????????????????? const(常量)
?
?
???????? 變量調(diào)節(jié)器
?????????????????? escape(跟php中的htmlentities())
??????????????????????????? $smarty->assign('title','<h3>標(biāo)題</h3>')
??????????????????????????? {$title}則直接輸出 html的格式
??????????????????????????? {$title|escape}???????? 轉(zhuǎn)碼
??????????????????????????? escape($title)
?
?????????????????? default
?????????????????? 當(dāng)沒有的時(shí)候則是默認(rèn)值
????????
?????????????????? date_format
??????????????????????????? {$smarty.now|date_format:"Y-m-d H:i:s"}
?
?????????????????? truncate(截取長度)
??????????????????????????? {"標(biāo)題標(biāo)題"|truncate:8}
?
?????????????????? upper(轉(zhuǎn)大寫)
??????????????????????????? {"lamp"|upper}
?
?????????????????? strtoupper
??????????????????????????? {"lamp"|strtoupper}
?
?????????????????? {"="|str_repeat:40}????????
?
?????????????????? php中的函數(shù)可以直接作為變量調(diào)節(jié)器使用
?????????????????? 自定義函數(shù)也可以
?
?
???????? 內(nèi)置函數(shù)
?????????????????? foreach
??????????????????????????? {foreach $stulist as $stu}
???????????????????????????????????? <tr>
?????????????????????????????????????????????? <td>{$stu@index+1}</td>
?????????????????????????????????????????????? <td>{$stu.name}</td>
???????????????????????????????????? </tr>
??????????????????????????? {/foreach}????????
?????????????????? if elseif else
?????????????????? while
?????????????????? include
?????????????????? for
??????????????????????????? (0-100的偶數(shù))
??????????????????????????? {for $i=1 to 100 step 2}
???????????????????????????????????? ?{i}
??????????????????????????? {/for}????????
?
A.???? Smarty配置
????????
?????????????????? 定義定界符(屬性)
??????????????????????????? $left_delimiter[左定界符]
??????????????????????????? $right_delimiter[右定界符]
???????????????????????????
??????????????????????????? $smarty->left_delimiter='<{';
??????????????????????????? $smarty->right_delimiter='}>';
?
?????????????????? 定義模版目錄
??????????????????????????? $template_dir[模板目錄]
?
??????????????????????????? $smarty->template_dir='./view';//屬性
??????????????????????????? $smarty->setTemplateDir('./view');//方法
??????????????????
?????????????????? 定義編譯的目錄
??????????????????????????? $smarty->setCompileDir('./view_c');
??????????????????????????? 編譯目錄不存在則自己創(chuàng)建
?
?????????????????? 緩存目錄
??????????????????????????? $smarty->setCachrDir('./cache')???? ;
?
?????????????????? 配置文件的目錄
??????????????????????????? $smarty->setConfigDir('./config');
?
B.???? Smarty緩存
?????????????????? $smarty->conpile_check? [編譯檢查]
?????????????????? $smarty->conpile_compile? [強(qiáng)制編譯]
?
?????????????????? 1.開啟緩存
?????????????????? $caching
?????????????????? $smarty->caching=1;??????
?
?
?????????????????? 接受參數(shù)
?????????????????? $id=intval($_GET['id'])l
?????????????????? //判斷緩存是否有效,把文章ID作為緩存ID
?????????????????? //同一個(gè)模版,就可以有多個(gè)緩存文件
?????????????????? if(!smarty->isCached('article.tpl',$id)){
??????????????????????????? $data=array(
???????????????????????????????????? 2=>array('id'=>2,'title')
??????????????????????????? )
?????????????????? }
?
?
?????????????????? 2.緩存時(shí)間
?????????????????? $smarty->cache_lifetime=50? //秒表 默認(rèn)是3600??????
?
?????????????????? 3.//判斷是否有緩存
?????????????????? if(!$smarty->isCached('1.tpl')){
??????????????????????????? //有可能是從數(shù)據(jù)庫查詢
??????????????????????????? //分配變量
??????????????????????????? $smarty->assign('title','標(biāo)題')
??????????????????????????? //當(dāng)前時(shí)間
??????????????????????????? $smarty->assign('now',date('Y-m-d H:i:s'));
?????????????????? }
??????????????????
?????????????????? 4.$smarty->display('article.tpl',$id)
?
?????????????????? 不想緩存的地方
?????????????????? {nocache}
??????????????????????????? {$smarty.now|date_format}
?????????????????? {/nocache}
?
?????????????????? //清除某一個(gè)模版的緩存
?????????????????? $smarty->clearCache('article.tpl')
?????????????????? //清除某一個(gè)模版的ID的緩存
?????????????????? $smarty->clearCache('article.tpl',2)
?
??????????????????
C.???? Smarty插件
?????????????????? 第一種方法
?????????????????? //將自定義函數(shù)注冊為變量調(diào)節(jié)器
?????????????????? //第一個(gè)參數(shù)是插件的類型(9種)
?????????????????? //第二個(gè)參數(shù)是在Smarty中使用的名字
?????????????????? //第三個(gè)參數(shù)是我們自定義的函數(shù)名字
?????????????????? $smarty->registerPlugin('modifier','wanghaoyu',myfun)
?
?????????????????? 第二種方法
?????????????????? //添加一個(gè)插件目錄
?????????????????? //將特定命名的插件放到目錄中
?????????????????? //規(guī)則:類型前綴.函數(shù)名.php(9中類型前綴)
?????????????????? $smarty->addPluginDir('./myPlugins')
?????????????????? 自定義函數(shù)的 smarty_modifier_前綴不能改
?
?
?
D.???? Smarty繼承??? extends
?????????????????? display('index.html')
?????????????????? 在index代碼中寫上
??????????????????????????? {extends 'base.html'}
??????????????????????????? {block name='main'}
???????????????????????????????????? <div>
?????????????????????????????????????????????? index的內(nèi)容
???????????????????????????????????? </div>
??????????????????????????? {/block}
?????????????????? 在base.html想改的區(qū)域
??????????????????????????? {block name='main'}
?
??????????????????????????? {/block}
?????????????????? 唯一能做的就是重寫某一塊
?
?????????????????? 2.在block里面追加內(nèi)容
?????????????????? {block name='top' append}
??????????????????????????? <div>haha</div>
?????????????????? {/block}
?
?????????????????? 3.某個(gè)部分的背景變色,當(dāng)內(nèi)容變的時(shí)候同時(shí)變
?????????????????? {block name='top'}??
??????????????????????????? <div class='top'>
???????????????????????????????????? {$smarty.block.parent}
??????????????????????????? </div>
?????????????????? {/block}????
?
?????????????????? 4.title
?????????????????? 模版
?????????????????? {block name='head'}
??????????????????????????? <head>
???????????????????????????????????? <title>{$smarty.block.child}</title>
??????????????????????????? </head>??
?????????????????? {/block}????
??????????????????
?????????????????? 顯示頁
?????????????????? {block name='head'}文章{/block}
?
?
E.????? Smarty 的include??
????????
????????
????????
????????
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的Smarty s02的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LFI 命令执行
- 下一篇: jdk动态代理与cglib动态代理例子