繁体   English   中英

在smarty中包含页眉和页脚文件的最佳方法

[英]Best approach to include header and footer files in smarty

我刚刚开始使用smarty。 我只是有点困惑如何将文件联合在一起。 简而言之,我想将页眉和页脚加入其他文件中,例如索引和发布

所以我创建了header.php和header.tpl并将下面的代码添加到header.php

$header_template = new Smarty;
$header_template->debugging = false;
$header_template->caching = false;
$header_template->setTemplateDir('./templates/');
$header_template->assign("title", $home_title);
$header_template->assign("description", $output_cities);
$header_template->display('header.tpl');

然后,我在index中创建了一个index.php和index.tpl文件。 PHP添加下面的代码

include('header.php');
$index_template = new Smarty;
$index_template->debugging = false;
$index_template->caching = false;
$index_template->setTemplateDir('./templates/');
$index_template->assign("posts", $post);
$index_template->display('index.tpl');

但是在灵巧的文档中,他们要求使用{include file=header.tpl'}所以我不确定我是否以正确的方式进行操作。 感谢您能给我的任何答案。

index.php以这种方式进行操作:

$index_template = new Smarty;
$index_template->debugging = false;
$index_template->caching = false;
$index_template->setTemplateDir('./templates/');
$index_template->assign("posts", $post);
$index_template->assign("title", $home_title);//from header.php
$index_template->assign("description", $output_cities);//from header.php
$index_template->display('index.tpl');

然后在index.tpl

 {include file='header.tpl'}
 index template code
 {include file='footer.tpl'}

您不需要header.phpfooter.php

也许您应该使用更好的变量名,例如header_title而不是title ,以防止覆盖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM