Twig活动导航栏项

时间:2017-08-21 09:19:49

标签: php twig

index.php中的

switch($page){
    case 'home':
        echo $twig->render('home.twig');
        break;
    case 'iletisim':
        echo $twig->render('iletisim.twig');
        break;
    case 'hakkimizda':
        echo $twig->render('hakkimizda.twig');

在layout.twig

                <nav id="primary-menu" class="serif normal">
                    <ul>
                        <li class="current"><a href="index.php?p=home"><div>Ana Sayfa</div></a></li>      
                        <li><a href="index.php?p=hakkimizda"><div>Hakkımızda</div></a></li>                     
                        <li><a href="index.php?p=konaklama"><div>Konaklama</div></a>
                        </li>                            
                        <li><a href="#"><div>Çocuk &amp; Aile</div></a>
                            <ul>                               
                                <li><a href="index.php?p=oyun_alanlari"><div>Oyun Alanları</div></a></li>                                          
                                <li><a href="index.php?p=piknik_alanlari"><div>Piknik Alanları</div></a></li>                                 
                            </ul>                            


                    </ul>
                </nav><!-- #primary-menu end -->

我希望在页面更改时在导航栏中处于活动状态,我该怎么做

1 个答案:

答案 0 :(得分:2)

你可以这样做:

index.php中的

switch($page){
    case 'home':
        echo $twig->render('home.twig', ['current' => 'home']);
        break;
    case 'iletisim':
        echo $twig->render('iletisim.twig', ['current' => 'iletisim']);
        break;
    case 'hakkimizda':
        echo $twig->render('hakkimizda.twig', ['current' => 'hakkimizda']);
    ....

并在layout.twig

<nav id="primary-menu" class="serif normal">
    <ul>
        <li {{ (current == 'home') ? 'class="current"' : '' }}><a href="index.php?p=home"><div>Ana Sayfa</div></a></li>      
        <li {{ (current == 'hakkimizda') ? 'class="current"' : '' }}><a href="index.php?p=hakkimizda"><div>Hakkımızda</div></a></li>                     
        <li {{ (current == 'konaklama') ? 'class="current"' : '' }}><a href="index.php?p=konaklama"><div>Konaklama</div></a></li>                            
...

当然,你可以通过这种方式改进。