IF/ELSE for header.tpl



我在一个对SEO不友好的网站上工作。具体来说,header.tpl会自动插入到每个页面中,没有根据页面内容进行更改的选项。即,无论类别=浴室还是类别=厨房,等等

所以我需要一个if/else命令,但在这种情况下很难弄清楚它,再加上随之而来的更改。

portfolio_category.php页面上的代码如下,需要基于vf_category更改的是parts/header.tpl(我可以创建Bathroomheader.tpl、Kitchensheader.tpl等,以便相关的tpl具有该页面的相关Title和Description标记)。

<?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

这个页面也有自己的标题。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}

如有任何指导,我们将不胜感激!我只是想让parts/header.tpl根据从数据库中提取的vf_category更改为parts/Bathroomheader.tpl或parts/Kitchenheader.tpl。但这让我发疯了。

提前感谢,Gary

有几种方法可以做到这一点,但为了最大限度地减少更改的文件数量,我建议您:a) 从数据库中提取变量,并将其分配给第一个php文件上的smarty,用于头和正文:

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

b) 编辑您的文件头.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}

最新更新