MyBB Community Forums

Full Version: PHP in Template - What's the solution?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What I want to do is modify the header template and put in some custom php.

There may be an easier way to do this, but specifically I want to add a "current" class to the navigation list item for the page that myBB is on.
I saw that there's a $current_page in global.php, so a few if-statements would probably suffice. I just don't know where to put them.

From what I've read, myBB separates the php from the template as a security measure. The posts I found were from an ancient version (like 1.2), so I don't know if this has changed.

But how/where would I go to modify the nav? I prefer to do this in php rather than JavaScript.
You can get the php in templates plugin from MYBBhacks

http://mybbhacks.zingaburga.com/showthread.php?tid=260
can you give a specific example of what you want to do? the header variables and output can be modified via hooks through a plugin. you may be able to do what you want without adding php to templates
Basically, in the header template, there's a nav bar as a list, something like:
<ul id="nav">
   <li>portal</li>
   <li>forum</li>
   <li>foo</li>
   <li>bar</li>
</li>

What I want to do is apply a class to the current list item, depending on the current page.
If I'm on page foo.php it would look like this:
<ul id="nav">
   <li>portal</li>
   <li>forum</li>
   <li class="current">foo</li>
   <li>bar</li>
</li>

There's going to need to be some sort of lookup mapping because something like index.php is going to have to map to portal or some other page.

Basically some sort of logic controls and html editing are going to need to be done. I prefer to do this during html construction, through php.
use the hook global_start and set a variable that defines the class you want. then insert that class variable into the header template

jist of the plugin:
$plugins->add_hook('global_start', 'myplugin_set_class');

function myplugin_set_class
{
    global $myspecialclass;

    if(THIS_SCRIPT == "some_filename.php")
    {
        $myspecialclass = "class_speclal";
    }
    else
    {
        $myspecialclass = "class_normal";
    }
}

header template:
<ul id="nav">
   <li>portal</li>
   <li>forum</li>
   <li class="{$myspecialclass}">foo</li>
   <li>bar</li>
</li>

otherwise you can mess with the php in templates plugin listed above.
(2010-11-06, 02:10 AM)pavemen Wrote: [ -> ]use the hook global_start and set a variable that defines the class you want. then insert that class variable into the header template

otherwise you can mess with the php in templates plugin listed above.

Where exactly would I place that? In global.php?

Additionally, I don't think that would work. Each of the list items would have a class; either "default" or "current". Your way would apply the same class to each.
<ul id="nav">
   <li class="{$myspecialclass}">portal</li>
   <li class="{$myspecialclass}">forum</li>
   <li class="{$myspecialclass}">foo</li>
   <li class="{$myspecialclass}">bar</li>
</li>

The desired result would be:
<ul id="nav">
   <li class="default">portal</li>
   <li class="default">forum</li>
   <li class="current">foo</li>
   <li class="default">bar</li>
</li>

But if I'm on page bar.php, I'd want bar's class to be current and foo's class to be default.
I think inline if statement is the easiest way. Perhaps generating the whole nav portion in an external variable would be better?

You don't really need PHP for this, it can be done with just CSS and HTML.
You can also modify pavemen script this way :

$plugins->add_hook('global_start', 'myplugin_set_class');

function myplugin_set_class
{
    global $myspecialclass;

    if(THIS_SCRIPT == "filename1.php")
    {
        $class1 = "current";
    }
    else
    {
        $class1 = "default";
    }
    if(THIS_SCRIPT == "filename2.php")
    {
        $class2 = "current";
    }
    else
    {
        $class2 = "default";
    }
    if(THIS_SCRIPT == "filename3.php")
    {
        $class3 = "current";
    }
    else
    {
        $class3 = "default";
    }
    if(THIS_SCRIPT == "filename4.php")
    {
        $class4 = "current";
    }
    else
    {
        $class4 = "default";
    }
} 

header template:
<ul id="nav">
   <li class="{$class1}">portal</li>
   <li class="{$class2}">forum</li>
   <li class="{$class3}">foo</li>
   <li class="{$class4}">bar</li>
</li> 
(2010-11-06, 04:34 PM)AJS Wrote: [ -> ]You don't really need PHP for this, it can be done with just CSS and HTML.

@AJS:
Could you please explain a cross-browser w3c compliant way of using css/html for this. Building the HTML document is not something CSS was designed to do and I prefer not to do it after page load. The correct way to do something like this is through PHP, whether in the template or externally.

@exdiogene:
While that may cause creating many variables and not being as robust, I think it might be the best way. I rather not use the PHP in Template plugin suggested above, because of the performance implications.

I will try this method out and report back on my findings.

Thank you all,
vol7ron

The following worked; please let me know if you have other recommendations:

global.php - I placed it above where $current_page is set, around where send_page_headers() is performed
$plugins->add_hook('global_start', 'setNavClass');

function setNavClass ()
{
    global $pageStatus;
    $class = "current";

    if(THIS_SCRIPT == "portal.php")              { $pageStatus['portal']  = $class; }
    elseif( THIS_SCRIPT == "index.php"
         || THIS_SCRIPT == "forumdisplay.php"
         || THIS_SCRIPT == "showthread.php"
          )                                      { $pageStatus['forums']  = $class; }
    elseif( THIS_SCRIPT == "search.php")         { $pageStatus['search']  = $class; }
    elseif( THIS_SCRIPT == "memberlist.php"
         || THIS_SCRIPT == "member.php")         { $pageStatus['members'] = $class; }
} 

This is okay for what I need, otherwise I'd predefine the array members with a default class.

I still would have liked to compare the page to the list item text with regex, to have a more robust solution.
that is why my post said "jist of the plugin"

putting it in global.php is a better solution as it does not require the overhead of a plugin. however, just remember to redo the edits after an upgrade