久艹网,亚洲一日韩欧美中文字幕2019,国产欧美日韩精品专区黑人,一区二区三区久久99

中山php|最優(yōu)網(wǎng)絡(luò)中山做網(wǎng)站 中山php建站

最優(yōu)良人

2012/09/22 at 11:16

好用的smarty標(biāo)簽:capture,literal,fetch

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開(kāi)發(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ī)開(kāi)發(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}

 

 

標(biāo)簽:
-