Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Smarty include有什么用

Smarty include有什么用

PHPz
PHPzasal
2016-06-06 20:23:101825semak imbas

Smarty include有什么用

Smarty include怎么用?

{include}用于载入其他模板到当前模板中。 在包含模板中可用的变量,载入后在当前模板仍然可用。

{include}必须设置file 属性,设置载入的文件资源路径。

设置了可选的assign属性,将{include}模板的内容赋值到变量,而并非输出。 与{assign}操作相似。

包含模板时,可以像使用属性一样设置传递的变量。 这样传递的变量,作用范围仅限于包含的模板内。 属性传递的变量将覆盖原包含模板的同名变量。

你可以在当前模板内使用包含模板的全部变量。 但是如果包含模板内有修改或者新建变量,那么这些变量只有包含模板的作用范围,而不可以是当前{include}模板中使用。 这种默认的设置,可以通过在包含模板时设置{include}的作用范围属性,或者 在修改或新增变量时通过{assign}的作用范围属性来设定。 后者在需要包含模板返回值时比较有用。

当文件不在$template_dir目录中时, 使用资源的语法来{include}包含文件。

使用例子:

<html>
<head>
  <title>{$title}</title>
</head>
<body>
{include file=&#39;page_header.tpl&#39;}
 
{* body of template goes here, the $tpl_name variable
   is replaced with a value eg &#39;contact.tpl&#39;
*}
{include file="$tpl_name.tpl"}
 
{* using shortform file attribute *}
{include &#39;page_footer.tpl&#39;}
</body>
</html>

include使用:

在php文件中可以这么写:

 

 $header=$path.&#39;header.dwt&#39;;
 $smarty->assign(&#39;header&#39;,$header);

在模板文件中则这样写:

 

{include file=$header}

更多相关知识,请访问PHP中文网

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn