嗨,我在每个页面上的网站上都有一个菜单,我想将其放在自己的menu.php文件中,但是我不确定如何为我所在的任何页面设置 class =“ active” 。这是我的代码:请帮助我
menu.php:
<li class=" has-sub"> <a class="" href="javascript:;"><i class=" icon-time"></i> Zeiten<span class="arrow"></span></a> <ul class="sub"> <li><a class="" href="offnungszeiten.php">Öffnungszeiten</a></li> <li><a class="" href="sauna.php">Sauna</a></li> <li><a class="" href="frauensauna.php">Frauensauna</a></li> <li class=""><a class="" href="custom.php">Beauty Lounge</a></li> <li><a class="" href="feiertage.php">Feiertage</a></li> </ul> </li>
如果您要在脚本中构建一个页面数组并将其与当前活动页面一起传递到视图文件,将会更容易:
//index.php or controller $pages = array(); $pages["offnungszeiten.php"] = "Öffnungszeiten"; $pages["sauna.php"] = "Sauna"; $pages["frauensauna.php"] = "Frauensauna"; $pages["custom.php"] = "Beauty Lounge"; $pages["feiertage.php"] = "Feiertage"; $activePage = "offnungszeiten.php"; //menu.php <?php foreach($pages as $url=>$title):?> <li> <a <?php if($url === $activePage):?>class="active"<?php endif;?> href="<?php echo $url;?>"> <?php echo $title;?> </a> </li> <?php endforeach;?>
使用像Smarty这样的模板引擎,您的menu.php看起来会更好:
//menu.php {foreach $pages as $url=>$title} <li> <a {if $url === $activePage}class="active"{/if} href="{$url}"> {$title} </a> </li> {/foreach}