2012-12-17 46 views
1

我在一個不友善的網站上工作。具體來說,header.tpl會自動插入到每個頁面中,無法根據頁面內容進行更改。即類別=浴室或類別=廚房等。IF/ELSE for header.tpl

因此,我需要一個if/else命令,但在這種情況下無法計算出它,以及隨之而來的變化。

portfolio_category.php頁面上的代碼如下,需要根據vf_category改變的是parts/header.tpl(我可以創建Bathroomheader.tpl,Kitchensheader.tpl等,以便相關的tpl具有相關的頁面的標題和描述標籤)。

<?php 
$vc_root_friendly = '.'; 
$vc_root_site = '.'; 
include_once("$vc_root_site/config.php"); 

include_once("$vc_includes_folder/IT.php"); 
$template_body = new HTML_Template_IT(); 
$template_body->loadTemplateFile("tpl_portfolio_category.html"); 

include_once("$vc_includes_folder/images.php"); 

if(!isset($_REQUEST['vf_category'])){ 
header("location:portfolio.php"); 
die(); 
} 

//Show header 
$template_header = new HTML_Template_IT(); 
$template_header->loadTemplateFile("parts/header.tpl",true,false); 
$template_body->setVariable('header', $template_header->get()); 

//Show footer 
$template_footer = new HTML_Template_IT(); 
$template_footer->loadTemplateFile("parts/footer.tpl",true,false); 
$template_body->setVariable('footer', $template_footer->get()); 


$template_body->setVariable("image_category", $_REQUEST['vf_category']); 

//Select photos for this category 
etc. 

事情複雜沒有在上面的代碼中引用另一頁:

tpl_portfolio_category.html 

,而這個頁面也有自己的header.tpl包括:

<? include_once 'parts/header.tpl'; ?> 
{header} 
<table width="100%" cellspacing="0" cellpadding="0" border="0"> 
<tr> 
<td class="main"><h1><span class="firstLetter">{image_category}</span></h1> 
    <p> 
    </p> 
    <table height="89" border="0" align="center" cellpadding="0" cellspacing="0"> 
    <tr> 
     <td colspan="7">&nbsp;</td> 
    </tr> 
    <!-- BEGIN block_thumb --> 
    <tr> 
     <td width='180' height="120" background="./images/thumb-bg.gif"> <div    

align="center">{thumb1}</div></td> 
     <td width="10">&nbsp;</td> 
     <td width='180' background="./images/thumb-bg.gif"> <div align="center">{thumb2}  

</div></td> 
     <td width="10">&nbsp;</td> 
     <td width='180' background="./images/thumb-bg.gif"> <div align="center">{thumb3} 

</div></td> 
     <td width="10">&nbsp;</td> 

    <td width='180' background="./images/thumb-bg.gif"> <div align="center">{thumb4} 
    </div></td> 
    </tr> 
    <tr valign="bottom"> 
     <td height="3"></td> 
     <td height="3"></td> 
     <td height="3"></td> 
     <td height="3"></td> 
     <td height="3"></td> 
     <td height="3"></td> 
     <td height="3"></td> 
    </tr> 
    <!-- END block_thumb --> 
    </table> 
    <br> 
    <img src="images/spacer.gif"></td> 
    </tr> 
    </table> 
    {footer} 

任何指導,將不勝感激!我只是試圖根據從數據庫中提取的vf_category將parts/header.tpl更改爲parts/Bathroomheader.tpl或parts/Kitchenheader.tpl。但是,這讓我瘋狂。

由於提前, 加里

+0

有關PHP和Smarty問題的「if-statement」標籤?問:你的問題究竟是什麼?你能澄清嗎? – paulsm4

回答

1

有幾個方法可以做到這一點,但爲了最大限度地減少你改變文件的數量,我建議你: 一)從拉變量數據庫,並將其分配給智者,你的第一個PHP文件,標題和正文都:

//Assuming you have retrieved $vf_category from your database; 
$template_header->setVariable('vf_category', $vf_category); 
$template_body->setVariable('vf_category', $vf_category); 

二)編輯文件header.tpl並追加在頂部下面的代碼:

{if 'vf_category' == 'some_value'} 
    {include file="parts/Kitchenheader.tpl"}; 
{elseif 'vf_category' == 'other_value'} 
    {include file="parts/Bathroomheader.tpl"}; 
{else} 
    //the rest of the header.tpl code goes here 
{/if} 
+0

謝謝。我會試試這個報告。 – user1908748