最優(yōu)良人 » smarty http://www.dgkai.cn/blog 中山php|最優(yōu)網(wǎng)絡(luò) Mon, 13 May 2013 04:56:43 +0000 en hourly 1 http://wordpress.org/?v=3.1.4 smarty模版使用php標(biāo)簽,如何獲取模版變量 http://www.dgkai.cn/blog/view-409.html http://www.dgkai.cn/blog/view-409.html#comments Sat, 22 Sep 2012 03:54:23 +0000 lin http://www.dgkai.cn/blog/?p=409 已經(jīng)assign一個(gè)模版變量$assign,由于要做特殊的循環(huán)輸出,使用for循環(huán),因此使用到了php標(biāo)簽,但是php語(yǔ)句和模版語(yǔ)句的變量作用域是不同的,因此不能直接獲取到

{{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的一個(gè)對(duì)象里面;只要在for之前進(jìn)行賦值:$assign = $this->_tpl_vars[assign];

{{php}}
$assign = $this->_tpl_vars[assign];
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}}

]]>
http://www.dgkai.cn/blog/view-409.html/feed 502
好用的smarty標(biāo)簽:capture,literal,fetch http://www.dgkai.cn/blog/view-407.html http://www.dgkai.cn/blog/view-407.html#comments Sat, 22 Sep 2012 03:16:24 +0000 lin http://www.dgkai.cn/blog/?p=407

1,capture標(biāo)簽

capture的中文意思是抓取,它的作用是抓取模板輸出的數(shù)據(jù),當(dāng)我們需要它的時(shí)候,調(diào)用它,以得到抓取數(shù)據(jù)的目的。例子:

  1. {capture?name=test}
  2. <img?src=”testimg.jpg”>
  3. {/capture}
  4. <div?class=”image”>
  5. {$smarty.capture.test}
  6. </div>

說(shuō)明:
在{capture name=”test”}和{/capture}之間的內(nèi)容被存儲(chǔ)到變量$test中,該變量由name屬性指定.在模板中通過(guò) $smarty.capture.test 訪問(wèn)該變量.如果沒(méi)有指定name 屬性,函數(shù)默認(rèn)將使用”default” 作為參數(shù),這一點(diǎn)很jquery中的clone

2,config_load標(biāo)簽

config_load可以直接將文件中的內(nèi)容讀取出來(lái),這樣可以省掉assign這一步。

  1. test.csv:
  2. pageTitle?=?”config_load_test”
  3. bodyBgColor?=?”#eeeeee”
  4. img?=?”girl.jpg”
  5. width=”100″
  6. height=”100″
  7. index.tpl:
  8. {config_load?file=”test.csv”}
  9. <html>
  10. <title>{#pageTitle#}</title>
  11. <body?bgcolor=”{#bodyBgColor#}”>
  12. <img?src=”{#img#}”?width=”{#width#}”?height=”{#height#}”>
  13. </body>
  14. </html>

上述過(guò)程中如果出現(xiàn)這樣的問(wèn)題Warning: Smarty error: unable to read resource, 請(qǐng)查看一下,你的test.csv是不是放在smarty的配置目錄中,默認(rèn)配置目錄是configs

  1. /**
  2. *?The?directory?where?config?files?are?located.
  3. *
  4. *?@var?string
  5. */
  6. var?$config_dir??????=??’configs’;

3,literal標(biāo)簽的使用

做web開發(fā),難免會(huì)寫一些JS,jquery代碼。js和jquery里面都會(huì){}這樣的符號(hào),smarty會(huì)不會(huì)把它理解成php的變量呢?如果你不加literal標(biāo)簽的話,smarty肯定會(huì)把它理解變量了,加了就不會(huì),例如:

  1. {literal}
  2. function?getAbsLeft(e){
  3. var?l=e.offsetLeft;
  4. while(e=e.offsetParent)l+=e.offsetLeft;
  5. return?l;
  6. }
  7. function?getAbsTop(e){
  8. var?t=e.offsetTop;
  9. while(e=e.offsetParent)t+=e.offsetTop;
  10. return?t;
  11. }
  12. {/literal}

4,php標(biāo)簽

當(dāng)你習(xí)慣了assign后,你有沒(méi)有想過(guò),在模板文件里面直接寫php代碼呢,我想有的時(shí)候你肯定很想吧。例如:

  1. {php}
  2. global?$result;
  3. foreach($result?as?$key=>$value){
  4. echo?”key=$key,value=>$value<br>”;
  5. }
  6. {/php}

5,strip標(biāo)簽

strip標(biāo)簽去除標(biāo)簽內(nèi)的空格和回車,這一點(diǎn)我覺(jué)得,做手機(jī)開發(fā)的朋友肯定用的到,因?yàn)槿强崭裼锌赡軙?huì)導(dǎo)致整個(gè)頁(yè)面錯(cuò)亂,甚至是一個(gè)空白頁(yè)面。手機(jī)屏幕小,估計(jì)用smarty的可能性也比較小。

  1. {strip}
  2. <div>
  3. <font?color=”red”>strip</font>
  4. </div>
  5. {/strip}

6,fetch標(biāo)簽

fetch標(biāo)簽根php的file_get_contents挺想的,都可以把文件中的內(nèi)容讀出來(lái),并且是個(gè)字符串的形勢(shì)

  1. {fetch?file=”./aaaa.txt”?assign=”result”}
  2. {if?is_array($result)}
  3. <b>is?array</b>
  4. {else?if}
  5. <b>not?array</b>
  6. {/if}

 

 

]]>
http://www.dgkai.cn/blog/view-407.html/feed 481
smarty 利用@ 在模版完整打印多維數(shù)組 http://www.dgkai.cn/blog/view-388.html http://www.dgkai.cn/blog/view-388.html#comments Sat, 21 Jul 2012 01:41:11 +0000 lin http://www.dgkai.cn/blog/?p=388 有時(shí)候我們希望直接在模版上打印數(shù)組變量以供調(diào)試,打印的方式可以用php自帶的print_r或者是自己寫的調(diào)試函數(shù),如debug().

如果直接這樣打印多維數(shù)組 {{$var|print_r}},在模版看到的結(jié)果會(huì)是遍歷后的所有的value,不會(huì)顯示完整的數(shù)組結(jié)構(gòu),正確的方法是在函數(shù)前加個(gè)@,意思是把變量作為整體去對(duì)待

{{$var|@print_r}}

]]>
http://www.dgkai.cn/blog/view-388.html/feed 613
smarty模版函數(shù)含多參數(shù)的使用規(guī)則 http://www.dgkai.cn/blog/view-322.html http://www.dgkai.cn/blog/view-322.html#comments Wed, 01 Feb 2012 09:38:50 +0000 lin http://www.dgkai.cn/blog/?p=322

 

模板中調(diào)用變量時(shí),當(dāng)只有一個(gè)參數(shù)是,就直接{$str1|函數(shù)名},當(dāng)有函數(shù)有兩個(gè)參數(shù)時(shí),{第一個(gè)參數(shù)|函數(shù)名:第二個(gè)參數(shù)},當(dāng)有三個(gè)參數(shù)時(shí),{第一個(gè)參數(shù)|函數(shù)名:第二個(gè)參數(shù):第三個(gè)參數(shù)},,當(dāng)有4,5,,,參數(shù)時(shí),以此類推。

smarty在模板上可以直接使用php自帶的函數(shù),甚至可以使用自定義的函數(shù)。

smarty使用date函數(shù)的用法是{{'Y-m-d'|date:$var}}

 

]]>
http://www.dgkai.cn/blog/view-322.html/feed 359
Smarty模版中,數(shù)組的鍵名是一個(gè)變量的值,如何顯示該鍵名對(duì)應(yīng)的值 http://www.dgkai.cn/blog/view-261.html http://www.dgkai.cn/blog/view-261.html#comments Thu, 08 Sep 2011 08:45:20 +0000 lin http://www.dgkai.cn/blog/?p=261 題目有點(diǎn)繞口,大概的意思是

php已經(jīng)賦給模版一個(gè)數(shù)組,數(shù)組的信息如下:

$config= array(

1=>'中山',

2=>'石岐'

);

數(shù)據(jù)庫(kù)存儲(chǔ)地區(qū)的字段記錄的是該數(shù)組的鍵名,如1,現(xiàn)在要在模版上顯示:中山。

如果這樣寫會(huì)報(bào)錯(cuò):

{{$config.$row.region}}

模版上的正確的寫法是:{{$config[$row.region]}}

今天遇到的問(wèn)題還更復(fù)雜一點(diǎn),數(shù)據(jù)庫(kù)字段存儲(chǔ)的是一些配置的序列化,所以在調(diào)取地區(qū)信息時(shí)還需要進(jìn)行反序列化處理,中間必須有一個(gè)賦值的過(guò)程:

{{assign var=param value=$l.params|unserialize}}

然后$param.region就可以取得1這個(gè)值了

]]>
http://www.dgkai.cn/blog/view-261.html/feed 210
smarty使用date函數(shù) http://www.dgkai.cn/blog/view-147.html http://www.dgkai.cn/blog/view-147.html#comments Fri, 19 Aug 2011 10:04:40 +0000 lin http://www.dgkai.cn/blog/?p=147 smarty在模板上可以直接使用php自帶的函數(shù),甚至可以使用自定義的函數(shù),使用的方法是:

模板中調(diào)用變量時(shí),當(dāng)只有一個(gè)參數(shù)是,就直接{$str1|函數(shù)名},當(dāng)有函數(shù)有兩個(gè)參數(shù)時(shí),{第一個(gè)參數(shù)|函數(shù)名:第二個(gè)參數(shù)},當(dāng)有三個(gè)參數(shù)時(shí),{第一個(gè)參數(shù)|函數(shù)名:第二個(gè)參數(shù):第三個(gè)參數(shù)},,當(dāng)有4,5,,,參數(shù)時(shí),以此類推。

smarty使用date函數(shù)的用法是{{'Y-m-d'|date:$var}}

]]>
http://www.dgkai.cn/blog/view-147.html/feed 5