2016-12-13 32 views
0

我剛開始使用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.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'} 所以我不知道我是否以正確的方式做這個。感謝您能給我的任何答案。

+0

有趣的錯字在你的第一個字... – JustOnUnderMillions

+0

@JustOnUnderMillions哈哈我的壞。抱歉。 – Jordyn

+0

基本上,當使用Smarty的時候,你不應該用include('header.php')包含頁面的一部分,',在模板中做所有的工作,當它關於組合模板 – JustOnUnderMillions

回答

1

做,在這種方式中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,以防止壓倒一切。

+0

好吧,但似乎我需要在所有其他的PHP文件中編寫頁眉和頁腳代碼。因爲我不能單獨包含它們。 – Jordyn

相關問題