Smarty 循环语句
Smarty 变量修饰器 

Smarty include

在 Smarty 模板引擎中,{include} 标签用于在一个模板文件中包含另一个模板文件。这种方法有助于将模板分解成更小、更可重用的部分,保持模板代码的组织性和可维护性。

基本用法

{include file="filename.tpl"}

这里 filename.tpl 是你要包含的模板文件的名称。这个文件通常包含公共的布局、头部、脚部或者其他模块化的内容。

使用示例

假设你有两个模板文件:header.tpl 和 footer.tpl,你可以在主模板文件中这样包含它们:

header.tpl

<!DOCTYPE html>
<html>
<head>
    <title>{$pageTitle}</title>
</head>
<body>
    <header>
        <h1>Welcome to {$siteName}</h1>
    </header>

footer.tpl

    <footer>
        <p>&copy; 2024 {$siteName}. All rights reserved.</p>
    </footer>
</body>
</html>

main.tpl

{include file="header.tpl"}
<main>
    <h2>{$mainHeading}</h2>
    <p>{$content}</p>
</main>
{include file="footer.tpl"}

传递变量

可以在 include 标签中传递变量,这些变量会在被包含的模板中使用。使用 assign 属性来实现这一点:

{include file="filename.tpl" var1=$value1 var2=$value2}

在 filename.tpl 中,你可以使用这些变量:

<p>Value 1: {$var1}</p>
<p>Value 2: {$var2}</p>

动态包含

如果需要动态地指定包含的模板,可以使用变量:

{include file=$templateName}


{include} 标签是 Smarty 中一个非常有用的功能,可以帮助你模块化和组织模板代码。它使得在不同模板文件间重用代码变得简单和高效,同时也支持传递变量、条件包含以及动态包含等高级功能。