?
Ce document utilise Manuel du site Web PHP chinois Libérer
This tip is a bit of a hack, but still a neat idea. Use at your own risk. ;-)
這項技巧有點普通,但是仍然是個不錯的思想,如果要使用的話,自擔風險。;-)
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)上,把模板編入你的應用程序中步驟如下:首先,你累計一下你的PHP程序中的變量,(可能用到數(shù)據(jù)庫查詢。)然后,你實例化一個你的Smarty對象,給變量賦值并且輸出這個模板。因此,例如我們說我們在我們的模板里有一個資源標記。我們可以在我們的應用程序中收集這些資源數(shù)據(jù),然后給模板中的這些變量賦值并輸出。現(xiàn)在如果你僅僅是通過包含這些模板就可以把資源標記加入到任何一個應用程序中不是很好嗎?
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.
你可以通過使用{php}{/php}這兩個標簽來在你的模板里嵌入PHP。這樣,你就能夠建立包含自身的使用自己的數(shù)據(jù)結(jié)構(gòu)的模板來給他們自身的變量賦值。通過像這樣邏輯的嵌入,你能夠把模板和邏輯性結(jié)合起來。這種方法使得模板不管源于何處總是像一個構(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 ...}這些標簽來包含php。使用這種方法,你能夠保持PHP的邏輯性和模板的邏輯性分離。參照include_php 這個函數(shù)來獲得更多信息。
Example 18-8. componentized template with include_php 例 18-8. 使用include_php組合的模板
|