Smarty include有什么用

PHPz
Release: 2020-09-04 15:22:12
Original
1823 people have browsed it

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>
Copy after login

include使用:

在php文件中可以这么写:

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

在模板文件中则这样写:

{include file=$header}
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!