How to set current page “active” in php

后端 未结 17 1846
栀梦
栀梦 2020-12-02 21:38

Hi I have a menu on my site on each page, I want to put it in it\'s own menu.php file but i\'m not sure how to set the class=\"active\" for whatever page i\

17条回答
  •  眼角桃花
    2020-12-02 22:12

    It would be easier if you would build an array of pages in your script and passed it to the view file along with the currently active page:

    //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
    $title):?>
      
  • class="active" href="">
  • With a templating engine like Smarty your menu.php would look even nicer:

    //menu.php
    {foreach $pages as $url=>$title}
       
  • {$title}
  • {/foreach}

提交回复
热议问题