Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Determined HTML Classes
    primarykey
    data
    text
    <p>I'm trying to determine the best way of using PHP to assign the classes of my tabbed menu items.</p> <p>What I've read so far on the subject here and elsewhere seems to only determine very simplistic cases. I need to have an active class for the tab when you're on it's page and I also need a disabled class for the tab on accounts without access to that feature (based on a session flag).</p> <p>Originally, I just echoed out everything like this:</p> <pre><code> &lt;?php echo '&lt;li class="some-tab ';if($pageName === "somepage.php"){echo'active';} if(!$_SESSION['some_flag']){echo'disabled'; echo'"&gt;&lt;a href=""&gt;Link&lt;/a&gt;';} ?&gt; </code></pre> <p>But then code shame set in and I'm trying to really start using PHP like templating engine. That said, I do not want to use an actual templating engine.</p> <p>For any other part of the HTML, hopping in and out of PHP is easy and relatively elegant, but when it comes to getting classes to work, it's feels clunky. It completely pollutes the markup and leaves me with way too much logic in my template.</p> <p>Like so:</p> <pre><code>&lt;li class="some-tab &lt;? if($pageName === "somepage.php"){echo'active';} ?&gt;&lt;/li&gt; </code></pre> <p>Not terrible but add a few more rules and it becomes a mess.</p> <p>My first thought is creating an associative array and just echoing it's contents:</p> <pre><code>&lt;? if(some condition){ $class = array("sometab" =&gt; "sometab active disabled"); } else{ $class = array("sometab" =&gt; "sometab"); } ?&gt; &lt;li class="&lt;?= $class['sometab'] ?&gt;"&gt; &lt;/li&gt; </code></pre> <p>But I feel like it may be <a href="http://en.wikipedia.org/wiki/KISS_principle" rel="nofollow">too clever</a>.</p> <p>I want to avoid storing information in the body class or as Javascript cookies as suggested by some because I'm not just fetching the URL.</p> <p>Is there an option I'm missing? </p> <p>Is a class array appropriate in the slightest? I don't have the experience to know if it's a horrible idea or not.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload