MyBB Community Forums

Full Version: Accessing global variables in a template
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi!

I really don't know, what I am doing wrong. I have modified the header-template like this:

<ul class="menu top_links">
<!-- rpgchar -->{$rpgchar_menu}<!-- /rpgchar -->{$menu_portal}
{$menu_search}

Now I have tried several hooks, let's just take this one as an example:

$plugins->add_hook("index_start", "rpgchar_makemenu");

And it jumps in the following function:

function rpgchar_makemenu()
{
global $db,$rpgchar_menu;

$rpgchar_menu = "<li><a href=\"{$mybb->settings['bburl']}/mypage.php\" class=\"help\">My page</a></li>";
}

What am I doing wrong? The template works, I get the remarks. And I could echo the $rpgchar_menu, so the hook seems to work too. But it seems that the variable is not global or the template could not access it. I first thought of a wrong variable name in the template.

I solved so many problems the last days and now I am stuck with this little last thing, I want to link my page… Could anybody help me please?
Why do you need to call it if your re-assigning it anyway ? Just return or echo it, depending on how your calling the function.

return $ rpgchar_menu;
I want to put different text in a template depending on a group-membership. A template could not call a function, but is able to use global variables, so you need to assign the wanted text to a global variable and put this one into the template. Just have a look in some templates and you'll understand what I mean.
You try using : $GLOBALS['variableName'] = "html here etc......"
For small scale template edits like this you should consider using template conditionals plugin

http://mybbhacks.zingaburga.com/showthread.php?tid=464
The headerinclude template gets eval'd before the index_start hook runs. You will need to use global_intermediate in 1.8 or global_start in 1.6.
(2015-01-12, 03:35 AM)Leefish Wrote: [ -> ]For small scale template edits like this you should consider using template conditionals plugin

http://mybbhacks.zingaburga.com/showthread.php?tid=464

Ah, interesting thing, I guess I have to bookmark it! But I try to work without using other plugins, don't like dependencies ;-)


(2015-01-12, 04:04 AM)Omar G. Wrote: [ -> ]The headerinclude template gets eval'd before the index_start hook runs. You will need to use global_intermediate in 1.8 or global_start in 1.6.

That's the solution! Thank you, that helped a lot, I was going to become mad about this problem!
Another method some authors use is to not use a variable name, but to put something like {username} and then hook to preoutput_page. They then use str_replace on that string.