Smarty s02
保留變量? 方便使用php
?????????????????? 不用assign
?????????????????? {$smarty}
?????????????????? get
??????????????????????????? {$smarty.get.page}
?????????????????? session
??????????????????????????? {$smarty.session.user.name}
?????????????????? server
?????????????????? cookies
?????????????????? request
?????????????????? const(常量)
?
?
???????? 變量調節器
?????????????????? escape(跟php中的htmlentities())
??????????????????????????? $smarty->assign('title','<h3>標題</h3>')
??????????????????????????? {$title}則直接輸出 html的格式
??????????????????????????? {$title|escape}???????? 轉碼
??????????????????????????? escape($title)
?
?????????????????? default
?????????????????? 當沒有的時候則是默認值
????????
?????????????????? date_format
??????????????????????????? {$smarty.now|date_format:"Y-m-d H:i:s"}
?
?????????????????? truncate(截取長度)
??????????????????????????? {"標題標題"|truncate:8}
?
?????????????????? upper(轉大寫)
??????????????????????????? {"lamp"|upper}
?
?????????????????? strtoupper
??????????????????????????? {"lamp"|strtoupper}
?
?????????????????? {"="|str_repeat:40}????????
?
?????????????????? php中的函數可以直接作為變量調節器使用
?????????????????? 自定義函數也可以
?
?
???????? 內置函數
?????????????????? 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的偶數)
??????????????????????????? {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');
??????????????????????????? 編譯目錄不存在則自己創建
?
?????????????????? 緩存目錄
??????????????????????????? $smarty->setCachrDir('./cache')???? ;
?
?????????????????? 配置文件的目錄
??????????????????????????? $smarty->setConfigDir('./config');
?
B.???? Smarty緩存
?????????????????? $smarty->conpile_check? [編譯檢查]
?????????????????? $smarty->conpile_compile? [強制編譯]
?
?????????????????? 1.開啟緩存
?????????????????? $caching
?????????????????? $smarty->caching=1;??????
?
?
?????????????????? 接受參數
?????????????????? $id=intval($_GET['id'])l
?????????????????? //判斷緩存是否有效,把文章ID作為緩存ID
?????????????????? //同一個模版,就可以有多個緩存文件
?????????????????? if(!smarty->isCached('article.tpl',$id)){
??????????????????????????? $data=array(
???????????????????????????????????? 2=>array('id'=>2,'title')
??????????????????????????? )
?????????????????? }
?
?
?????????????????? 2.緩存時間
?????????????????? $smarty->cache_lifetime=50? //秒表 默認是3600??????
?
?????????????????? 3.//判斷是否有緩存
?????????????????? if(!$smarty->isCached('1.tpl')){
??????????????????????????? //有可能是從數據庫查詢
??????????????????????????? //分配變量
??????????????????????????? $smarty->assign('title','標題')
??????????????????????????? //當前時間
??????????????????????????? $smarty->assign('now',date('Y-m-d H:i:s'));
?????????????????? }
??????????????????
?????????????????? 4.$smarty->display('article.tpl',$id)
?
?????????????????? 不想緩存的地方
?????????????????? {nocache}
??????????????????????????? {$smarty.now|date_format}
?????????????????? {/nocache}
?
?????????????????? //清除某一個模版的緩存
?????????????????? $smarty->clearCache('article.tpl')
?????????????????? //清除某一個模版的ID的緩存
?????????????????? $smarty->clearCache('article.tpl',2)
?
??????????????????
C.???? Smarty插件
?????????????????? 第一種方法
?????????????????? //將自定義函數注冊為變量調節器
?????????????????? //第一個參數是插件的類型(9種)
?????????????????? //第二個參數是在Smarty中使用的名字
?????????????????? //第三個參數是我們自定義的函數名字
?????????????????? $smarty->registerPlugin('modifier','wanghaoyu',myfun)
?
?????????????????? 第二種方法
?????????????????? //添加一個插件目錄
?????????????????? //將特定命名的插件放到目錄中
?????????????????? //規則:類型前綴.函數名.php(9中類型前綴)
?????????????????? $smarty->addPluginDir('./myPlugins')
?????????????????? 自定義函數的 smarty_modifier_前綴不能改
?
?
?
D.???? Smarty繼承??? extends
?????????????????? display('index.html')
?????????????????? 在index代碼中寫上
??????????????????????????? {extends 'base.html'}
??????????????????????????? {block name='main'}
???????????????????????????????????? <div>
?????????????????????????????????????????????? index的內容
???????????????????????????????????? </div>
??????????????????????????? {/block}
?????????????????? 在base.html想改的區域
??????????????????????????? {block name='main'}
?
??????????????????????????? {/block}
?????????????????? 唯一能做的就是重寫某一塊
?
?????????????????? 2.在block里面追加內容
?????????????????? {block name='top' append}
??????????????????????????? <div>haha</div>
?????????????????? {/block}
?
?????????????????? 3.某個部分的背景變色,當內容變的時候同時變
?????????????????? {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??
????????
????????
????????
????????
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的Smarty s02的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LFI 命令执行
- 下一篇: SQLPlus 在连接时通常有四种方式