{include}
{include}
用于载入其他模板到当前模板中。 在包含模板中可用的变量,载入后在当前模板仍然可用。
-
{include}
必须设置file
属性,设置载入的文件资源路径。 -
设置了可选的
assign
属性,将{include}
模板的内容赋值到变量,而并非输出。 与{assign}
操作相似。 -
包含模板时,可以像使用属性一样设置传递的变量。 这样传递的变量,作用范围仅限于包含的模板内。 属性传递的变量将覆盖原包含模板的同名变量。
-
你可以在当前模板内使用包含模板的全部变量。 但是如果包含模板内有修改或者新建变量,那么这些变量只有包含模板的作用范围,而不可以是当前
{include}
模板中使用。 这种默认的设置,可以通过在包含模板时设置{include}
的作用范围属性,或者 在修改或新增变量时通过{assign}
的作用范围属性来设定。 后者在需要包含模板返回值时比较有用。 -
当文件不在
$template_dir
目录中时, 使用资源的语法来{include}
包含文件。
使用例子:
<html> <head> <title>{$title}</title> </head> <body> {include file='page_header.tpl'} {* body of template goes here, the $tpl_name variable is replaced with a value eg 'contact.tpl' *} {include file="$tpl_name.tpl"} {* using shortform file attribute *} {include 'page_footer.tpl'} </body> </html>
include使用:
在php文件中可以这么写:
1 |
$header = $path . 'header.dwt' ; |
2 |
$smarty ->assign( 'header' , $header ); |
在模板文件中则这样写: