Smarty include


include

Attribute Name Type Required Default 描述
file string Yes n/a The name of the template file to include
assign string No n/a The name of the variable that the output of include will be assigned to
[var ...] [var type] No n/a variable to pass local to template

屬性 型別 是否必須 預設值 描述
file string Yes n/a 待包含的模板檔案名
assign string No n/a 該屬性指定一個變數儲存待包含模板的輸出
[var ...] [var type] No n/a 傳遞給待包含模板的本地引數,只在待包含模板中有效

 

Include 標籤用於在當前模板中包含其它模板. 當前模板中的變數在被包含的模板中可用. 必須指定 file 屬性,該屬性指明模板資源的位置.

如果設定了 assign 屬性,該屬性對應的變數名用於儲存待包含模板的輸出,這樣待包含模板的輸出就不會直接顯示了。


Example 7-6. function include
例 7-6. include 函式演示

{include file="header.tpl"}

{* body of template goes here *}

{include file="footer.tpl"}

 

可以在屬性中傳遞引數給待包含模板. 傳遞給待包含模板的引數只在待包含模板中可見. 如果傳遞的引數在待包含模板中有同名變數,那麼該變數被傳遞的引數替代.


Example 7-7. function include passing variables
例 7-7. 帶傳遞引數的 include 函式演示

{include file="header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"}

{* body of template goes here *}

{include file="footer.tpl" logo="http://my.domain.com/logo.gif"}

 

包含 $template_dir 檔案夾之外的模板請使用 模板資源 說明的格式.


Example 7-8. function include template resource examples
例 7-8. 使用外部模板資源的 include 函式演示

{* absolute filepath *}
{include file="/usr/local/include/templates/header.tpl"}

{* absolute filepath (same thing) *}
{include file="file:/usr/local/include/templates/header.tpl"}

{* windows absolute filepath (MUST use "file:" prefix) *}
{include file="file:C:/www/pub/templates/header.tpl"}

{* include from template resource named "db" *}
{include file="db:header.tpl"}