中山php|最優網絡 :中山做網站 中山php建站
最優良人
Posts Tagged With: smarty
smarty模版使用php標簽,如何獲取模版變量
2012/09/22 at 11:54 » Comments (502)
已經assign一個模版變量$assign,由于要做特殊的循環輸出,使用for循環,因此使用到了php標簽,但是php語句和模版語句的變量作用域是不同的,因此不能直接獲取到 {{php}} for($i=0;$i<count($assign);$i=$i+2){ echo ' <ul> <li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i][pic_id])).'" title="">'.$assign[$i][title].'</a></span> </li> <li> <span class="zz_pic"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title=""><img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""></a></span> <span class="zz_title"><a href="'._url('picture',array('col_key'=>'cert','pic_id'=>$assign[$i+1][pic_id])).'" title="">'.$assign[$i+1][title].'</a></span> </li>i> </ul>';} {{/php}} 解決的方法是:模版變量全部存在smarty的一個對象里面;只要在for之前進行賦值:$assign = $this->_tpl_vars[assign]; {{php}} $assign = $this->_tpl_vars[assign]; for($i=0;$i<count($assign);$i=$i+2){ echo ...more »好用的smarty標簽:capture,literal,fetch
2012/09/22 at 11:16 » Comments (481)
1,capture標簽 capture的中文意思是抓取,它的作用是抓取模板輸出的數據,當我們需要它的時候,調用它,以得到抓取數據的目的。例子: {capture?name=test} <img?src=”testimg.jpg”> {/capture} <div?class=”image”> {$smarty.capture.test} </div> 說明: 在{capture name=”test”}和{/capture}之間的內容被存儲到變量$test中,該變量由name屬性指定.在模板中通過 $smarty.capture.test 訪問該變量.如果沒有指定name 屬性,函數默認將使用”default” 作為參數,這一點很jquery中的clone 2,config_load標簽 config_load可以直接將文件中的內容讀取出來,這樣可以省掉assign這一步。 test.csv: pageTitle?=?”config_load_test” bodyBgColor?=?”#eeeeee” img?=?”girl.jpg” width=”100″ height=”100″ index.tpl: {config_load?file=”test.csv”} <html> <title>{#pageTitle#}</title> <body?bgcolor=”{#bodyBgColor#}”> <img?src=”{#img#}”?width=”{#width#}”?height=”{#height#}”> </body> </html> 上述過程中如果出現這樣的問題Warning: Smarty error: unable to read resource, 請查看一下,你的test.csv是不是放在smarty的配置目錄中,默認配置目錄是configs /** *?The?directory?where?config?files?are?located. * *?@var?string */ var?$config_dir??????=??’configs’; 3,literal標簽的使用 做web開發,難免會寫一些JS,jquery代碼。js和jquery里面都會{}這樣的符號,smarty會不會把它理解成php的變量呢?如果你不加literal標簽的話,smarty肯定會把它理解變量了,加了就不會,例如: {literal} function?getAbsLeft(e){ var?l=e.offsetLeft; while(e=e.offsetParent)l+=e.offsetLeft; return?l; } function?getAbsTop(e){ var?t=e.offsetTop; while(e=e.offsetParent)t+=e.offsetTop; return?t; } {/literal} 4,php標簽 當你習慣了assign后,你有沒有想過,在模板文件里面直接寫php代碼呢,我想有的時候你肯定很想吧。例如: {php} global?$result; foreach($result?as?$key=>$value){ echo?”key=$key,value=>$value<br>”; } {/php} 5,strip標簽 strip標簽去除標簽內的空格和回車,這一點我覺得,做手機開發的朋友肯定用的到,因為全角空格有可能會導致整個頁面錯亂,甚至是一個空白頁面。手機屏幕小,估計用smarty的可能性也比較小。 {strip} <div> <font?color=”red”>strip</font> </div> {/strip} 6,fetch標簽 fetch標簽根php的file_get_contents挺想的,都可以把文件中的內容讀出來,并且是個字符串的形勢 {fetch?file=”./aaaa.txt”?assign=”result”} {if?is_array($result)} <b>is?array</b> {else?if} <b>not?array</b> {/if} more »smarty 利用@ 在模版完整打印多維數組
2012/07/21 at 09:41 » Comments (613)
有時候我們希望直接在模版上打印數組變量以供調試,打印的方式可以用php自帶的print_r或者是自己寫的調試函數,如debug(). 如果直接這樣打印多維數組 {{$var|print_r}},在模版看到的結果會是遍歷后的所有的value,不會顯示完整的數組結構,正確的方法是在函數前加個@,意思是把變量作為整體去對待 {{$var|@print_r}} more »smarty模版函數含多參數的使用規則
2012/02/01 at 17:38 » Comments (359)
模板中調用變量時,當只有一個參數是,就直接{$str1|函數名},當有函數有兩個參數時,{第一個參數|函數名:第二個參數},當有三個參數時,{第一個參數|函數名:第二個參數:第三個參數},,當有4,5,,,參數時,以此類推。 smarty在模板上可以直接使用php自帶的函數,甚至可以使用自定義的函數。 smarty使用date函數的用法是{{'Y-m-d'|date:$var}} more »Smarty模版中,數組的鍵名是一個變量的值,如何顯示該鍵名對應的值
2011/09/08 at 16:45 » Comments (210)
題目有點繞口,大概的意思是 php已經賦給模版一個數組,數組的信息如下: $config= array( 1=>'中山', 2=>'石岐' ); 數據庫存儲地區的字段記錄的是該數組的鍵名,如1,現在要在模版上顯示:中山。 如果這樣寫會報錯: {{$config.$row.region}} 模版上的正確的寫法是:{{$config[$row.region]}} 今天遇到的問題還更復雜一點,數據庫字段存儲的是一些配置的序列化,所以在調取地區信息時還需要進行反序列化處理,中間必須有一個賦值的過程: {{assign var=param value=$l.params|unserialize}} 然后$param.region就可以取得1這個值了 more »smarty使用date函數
2011/08/19 at 18:04 » Comments (5)
smarty在模板上可以直接使用php自帶的函數,甚至可以使用自定義的函數,使用的方法是: 模板中調用變量時,當只有一個參數是,就直接{$str1|函數名},當有函數有兩個參數時,{第一個參數|函數名:第二個參數},當有三個參數時,{第一個參數|函數名:第二個參數:第三個參數},,當有4,5,,,參數時,以此類推。 smarty使用date函數的用法是{{'Y-m-d'|date:$var}} more »