如何使用PHP将class ='active'添加到html菜单 [英] How add class='active' to html menu with php

查看:96
本文介绍了如何使用PHP将class ='active'添加到html菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将html导航放到一个单独的php文件中,因此当我需要对其进行编辑时,我只需要对其进行一次编辑.当我想将活动类添加到活动页面时,问题就开始了.

I want to put my html navigation in a separate php file so when I need to edit it, I only have to edit it once. The problem starts when I want to add the class active to the active page.

我有三页和一个通用文件.

I've got three pages and one common file.

common.php:

common.php :

<?php 
$nav = <<<EOD
   <div id="nav">
        <ul>
           <li><a <? if($page == 'one'): ?> class="active"<? endif ?> href="index.php">Tab1</a>/</li>
           <li><a href="two.php">Tab2</a></li>
           <li><a href="three.php">Tab3</a></li>
       </ul>
    </div>
EOD;
?>

index.php: 这三个页面都是相同的,只是每个页面上的$页面不同.

index.php : All three pages are identical except their $page is different on each page.

  <?php
     $page = 'one';      
     require_once('common.php');
    ?>
    <html>
       <head></head>
       <body>
          <?php echo $nav; ?>
       </body>
    </html>

除非我在每页上放置导航,否则这根本行不通,但是将导航和所有页面分开的整个目的就毁了.

This simply won't work unless I put my nav on each page, but then the whole purpose of separating the nav from all pages is ruined.

我想实现的目标甚至有可能吗?我在做什么错了?

Is what I want to accomplish even possible? What am I doing wrong?

谢谢

执行此操作时,li内部的php代码似乎未运行,只是像是html一样被打印

When doing this, the php code inside the li don't seem to run, it's just being printed as if it was html

推荐答案

您的index.php代码正确.我在下面包含了common.php的更新代码,然后我将解释它们之间的区别.

Your index.php code is correct. I am including the updated code for common.php below then I will explain the differences.

<?php 
     $class = ($page == 'one') ? 'class="active"' : '';
     $nav = <<<EOD
        <div id="nav">
            <ul>
               <li><a $class href="index.php">Tab1</a>/</li>
               <li><a href="two.php">Tab2</a></li>
               <li><a href="three.php">Tab3</a></li>
           </ul>
        </div>
 EOD;
 ?>

第一个问题是,您需要确保heredoc的结束声明-EOD;-完全没有缩进.如果缩进,则会出现错误.

The first issue is that you need to make sure that the end declaration for your heredoc -- EOD; -- is not indented at all. If it is indented, then you will get errors.

对于您的问题,PHP代码未在Heredoc语句中运行,是因为您看错了.使用heredoc语句与关闭PHP标记不同.因此,您无需尝试重​​新打开它们.那对您无济于事. Heredoc语法的工作方式是,打开和关闭之间的所有内容均按所编写的完全相同,但变量除外.这些将替换为关联的值.我从heredoc中删除了您的逻辑,并使用一个三级函数来确定要使其更易于查看的类(尽管我不相信任何逻辑语句仍然可以在heredoc中使用)

As for your issue with the PHP code not running within the heredoc statement, that is because you are looking at it wrong. Using a heredoc statement is not the same as closing the PHP tags. As such, you do not need to try reopening them. That will do nothing for you. The way the heredoc syntax works is that everything between the opening and closing is displayed exactly as written with the exception of variables. Those are replaced with the associated value. I removed your logic from the heredoc and used a tertiary function to determine the class to make this easier to see (though I don't believe any logical statements will work within the heredoc anyway)

要了解Heredoc语法,它与在双引号()中包含它相同,但是不需要转义.因此,您的代码也可以这样编写:

To understand the heredoc syntax, it is the same as including it within double quotes ("), but without the need for escaping. So your code could also be written like this:

<?php 
     $class = ($page == 'one') ? 'class="active"' : '';
     $nav = "<div id=\"nav\">
            <ul>
               <li><a $class href=\"index.php\">Tab1</a>/</li>
               <li><a href=\"two.php\">Tab2</a></li>
               <li><a href=\"three.php\">Tab3</a></li>
           </ul>
        </div>";
 ?>

它将执行完全相同的操作,只是编写方式有所不同. Heredoc和字符串之间的另一个区别是,您可以在中间无法进入Heredoc的字符串中跳出.使用此逻辑,您可以生成以下代码:

It will do exactly the same thing, just is written somewhat differently. Another difference between heredoc and the string is that you can escape out of the string in the middle where you can't in the heredoc. Using this logic, you can produce the following code:

<?php 
     $nav = "<div id=\"nav\">
            <ul>
               <li><a ".(($page == 'one') ? 'class="active"' : '')." href=\"index.php\">Tab1</a>/</li>
               <li><a href=\"two.php\">Tab2</a></li>
               <li><a href=\"three.php\">Tab3</a></li>
           </ul>
        </div>";
 ?>

然后,您可以像最初想要的那样直接在字符串中包含逻辑.

Then you can include the logic directly in the string like you originally intended.

您选择的任何一种方法对脚本性能的影响都很小(如果有的话).它主要归结为偏好.无论哪种方式,您都需要确保了解它们的工作原理.

Whichever method you choose makes very little (if any) difference in the performance of the script. It mostly boils down to preference. Either way, you need to make sure you understand how each works.

这篇关于如何使用PHP将class ='active'添加到html菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆