?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
This tip is a bit of a hack, but still a neat idea. Use at your own risk. ;-)
這項(xiàng)技巧有點(diǎn)普通,但是仍然是個(gè)不錯(cuò)的思想,如果要使用的話(huà),自擔(dān)風(fēng)險(xiǎn)。;-)
Traditionally, programming templates into your applications goes as follows: First, you accumulate your variables within your PHP application, (maybe with database queries.) Then, you instantiate your Smarty object, assign the variables and display the template. So lets say for example we have a stock ticker on our template. We would collect the stock data in our application, then assign these variables in the template and display it. Now wouldn't it be nice if you could add this stock ticker to any application by merely including the template, and not worry about fetching the data up front?
傳統(tǒng)上,把模板編入你的應(yīng)用程序中步驟如下:首先,你累計(jì)一下你的PHP程序中的變量,(可能用到數(shù)據(jù)庫(kù)查詢(xún)。)然后,你實(shí)例化一個(gè)你的Smarty對(duì)象,給變量賦值并且輸出這個(gè)模板。因此,例如我們說(shuō)我們?cè)谖覀兊哪0謇镉幸粋€(gè)資源標(biāo)記。我們可以在我們的應(yīng)用程序中收集這些資源數(shù)據(jù),然后給模板中的這些變量賦值并輸出?,F(xiàn)在如果你僅僅是通過(guò)包含這些模板就可以把資源標(biāo)記加入到任何一個(gè)應(yīng)用程序中不是很好嗎?
You can embed PHP into your templates with the {php}{/php} tags. With this, you can setup self contained templates with their own data structures for assigning their own variables. With the logic embedded like this, you can keep the template & logic together. This way no matter where the template source is coming from, it is always together as one component.
你可以通過(guò)使用{php}{/php}這兩個(gè)標(biāo)簽來(lái)在你的模板里嵌入PHP。這樣,你就能夠建立包含自身的使用自己的數(shù)據(jù)結(jié)構(gòu)的模板來(lái)給他們自身的變量賦值。通過(guò)像這樣邏輯的嵌入,你能夠把模板和邏輯性結(jié)合起來(lái)。這種方法使得模板不管源于何處總是像一個(gè)構(gòu)件一樣組合在一起。
Example 18-7. componentized template 例 18-7. 組合的模板
|
As of Smarty 1.5.0, there is even a cleaner way. You can include php in your templates with the {include_php ...} tag. This way you can keep your PHP logic separated from the template logic. See the include_php function for more information.
就像Smarty 1.5.0,這里有一種更清晰的方法。你可以在你的模板里使用{include_php ...}這些標(biāo)簽來(lái)包含php。使用這種方法,你能夠保持PHP的邏輯性和模板的邏輯性分離。參照include_php 這個(gè)函數(shù)來(lái)獲得更多信息。
Example 18-8. componentized template with include_php 例 18-8. 使用include_php組合的模板
|