Search code examples
phpmenutabsmodx

Choosing what tab a user is on, accross multiple pages


I am currently creating a website with tabs.

http://imageshack.us/photo/my-images/16/chosentab.png/ (It wont let me embed the image)

As you can see, there is a tab, called classPvP that is selected, and it is done so by adding a "selected" in the tag's class. However, this website is going to be like a wiki, and as such have many different pages, and I don't want to have to create a seperate 'template' (if you will) for every page with a different selected tab, because then updating or changing something across all those templates would be very difficult.

I am trying to use this code to add in the desired class when you have chosen a tab:

<?php
//Defining the variables
$a="[[*menutitle]]";
if ($a=="classpvp"){
echo "selected";
} else {
echo $a;
} ?>

With the CMS (MODx) I'm using, [[*menutitle]] is replaced by whatever tab I want to have'selected'. However, for some reason, the if statement is not being run, but the echo is outputting that the value of $a is classpvp .

This is seriously confusing me, as to why the if statement isn't being run, yet the value is correct - Am I storing something poorly? (Sorry, I'm very new to PHP)

(Keeping in mind that [[TabSelectClassPvP]] is replaced with the php code) When used var_dump($a) (as suggested), the class turns from :

<li class="navTab [[TabSelectClassPvP]]">
ClassPvP
</li>

to:

<li class="navTab string(14) " classpvp"="">
ClassPvP
</li>

The exact php code being run is:

<?php
//Defining the variables
$a="[[*menutitle]]";
var_dump($a);
if ($a=="classpvp"){
echo "selected";
} else {
echo $a;
}

Thanks, Muffinjello

P.S. - I'm open to new ways of doing this, maybe something that retrieves information from an HTML ID?


Solution

  • use the WAyFinder package for your menus, it will automatically place the 'active' rule in your class attribute. http://rtfm.modx.com/display/ADDON/Wayfinder if you want to use something different the &hereClass is the c=value you want to specify in your WayFinder call.

    UPDATE

    if you want to use the menutitle of the current resource in your snippet there are 2 ways to do it, pass it in from the snippet call:

    <li class="navTab [[!TabSelectClassPvP? &a='[[*menutitle]]']]">
    

    And your snippet looks like:

    <?php
    // Defining the variables
    // $a="[[*menutitle]]";
    // var_dump($a);
    
    if ($a=="classpvp"){
    echo "selected";
    } else {
    echo $a;
    }
    

    [you probably should check to see if $a has been passed]

    or you can query the current page directly in your snippet:

    <?php
    // Defining the variables
    // $a="[[*menutitle]]";
    // var_dump($a);
    
    $a = $modx->resource->get('menutitle');
    
    if ($a=="classpvp"){
    echo "selected";
    } else {
    echo $a;
    }
    

    As the modx object always assumes 'the current resource' you can grab any of the resource fields o TVs in your snippets like that.