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"}


上一篇: Smarty foreach,foreachelse 下一篇: Smarty include_php