我有header.tpl文件包含在我运行Smarty的整个网站中。我需要一个php-if代码,当浏览器中的页面处于活动状态时,它会使某些导航菜单元素占用“current-menu-item”类。
<ul>
<li class="current-menu-item"><a href="http://localhost/dbk/index.php">Home</a> </li>
<li><a href="#">Company</a>
<ul class="sub-menu">
<li><a href="http://localhost/dbk/about.php">About Company</a></li>
<li><a href="http://localhost/dbk/team.php">Excuslive Team</a></li>
<li><a href="http://localhost/dbk/global-business.php">Global Business</a></li>
<li><a href="http://localhost/dbk/contract-manufacturing.php">Contract Manufacturing</a></li>
</ul>
</li>
<li><a href="#">Our Products</a>
<ul class="sub-menu">
<li><a href="http://localhost/dbk/pharmaceutical-products.php">Pharmaceutical Products</a></li>
<li><a href="http://localhost/dbk/products/herbal-and-food.php">Herbal & Food Supplements</a></li>
<li><a href="http://localhost/dbk/products/cosmetics.php">Cosmetics</a></li>
<li><a href="http://localhost/dbk/products/veterinary-products.php">Veterinary Products</a></li>
</ul>
</li>
<li><a href="http://localhost/dbk/blog">News Room</a></li>
<li><a href="http://localhost/dbk/careers.php">Careers</a> </li>
<li><a href="http://localhost/dbk/contacts.php">Contacts</a></li>
答案 0 :(得分:1)
在PHP脚本中,执行$smarty->assign("curPage", $currentPage);
之类的操作($currentPage
通过$_GET
或您的方式设置。)
我在模板文件中使用它:
{foreach from=$aMenu key=key item=value}
{assign var="class" value=""}
{if $curPage == $value}
{assign var="class" value=" class='current-menu-item'"}
{/if}
<li{$class}><a href="/{$value}">{$key}</a></li>
{/foreach}
class
变量,空。curPage
(记住来自PHP的$currentPage
?)是否与当前的foreach循环项匹配class='current-menu-item'
)。<li>
元素。答案 1 :(得分:0)
只需通过本地指定的tpl变量,即:
<li {if $page == "about"}class="current-menu-item"{/if}><a href="http://localhost/dbk/about.php">About Company</a></li>
<li {if $page == "team"}class="current-menu-item"{/if}><a href="http://localhost/dbk/team.php">Excuslive Team</a></li>