View Full Version : Fetching a custom template within a function
inthezone
04-19-2008, 04:38 AM
function displayHeader()
{
eval('$myheader = "' . fetch_template('myheader') . '";');
echo $myheader;
}
displayHeader();
I have a custom template named 'myheader'. When I try to call it with the above function, I get a blank page. However, if I replace "fetch_template('myheader')" with "fetch_template('header')" vBulletin's standard header template is displayed.
How would I go about calling my custom template from this function?
nighthalk
04-19-2008, 04:56 AM
<a href="https://vborg.vbsupport.ru/showthread.php?t=119933&highlight=fetch+custom+template" target="_blank">https://vborg.vbsupport.ru/showt...ustom+template</a>
That should help.
inthezone
04-19-2008, 05:23 AM
https://vborg.vbsupport.ru/showthread.php?t=119933&highlight=fetch+custom+template
That should help.
Interesting, but it doesn't seem to work.
Something else of note: if I take the code out of the function and call it inline, the custom template works fine. Maybe that helps...
inthezone
04-20-2008, 05:46 AM
Turns out that 'myheader' worked in the function. The problem was that 'myheader' contains references to vB's default header template (as '$header'), and that wasn't working within the function. To get around this, I have to manually call eval('$header = "' . fetch_template('header') . '";'); within the function.
This brings me to my new question: I have some plugin code in the global_start hook that works fine, unless I call it from within a function. How would I go about doing this?
Opserty
04-20-2008, 08:51 AM
If you want to access variable define outside a function you need to use the global keyword to enable it to work inside the function. For example:
function displayHeader()
{
global $header;
eval('$myheader = "' . fetch_template('myheader') . '";');
echo $myheader;
}
Dismounted
04-20-2008, 08:58 AM
No need for the colon (does it even work that way?), it should be simply:
global $header;
Opserty
04-20-2008, 09:00 AM
Oops my bad. Wasn't concentrating. :p
inthezone
04-20-2008, 11:02 PM
Thanks! A new challenge, though...
I'm now trying to fetch $myheader through a parse_templates plugin, so I no longer have to include that line in my function:
// Code in vB's plugin system
eval('$myheader = "' . fetch_template('myheader') . '";');
// myheader template code in vB
$stylevar[htmldoctype]
<html id="moooo" dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
$headinclude
<title>$vboptions[bbtitle] - $pagetitle</title>
</head>
<body>
$header
// Function in my code, separate from vB
function displayHeader()
{
global $myheader, $header;
echo $myheader;
}
In my function, $myheader can't see the $header variable and $header isn't displayed. What am I missing?
Dismounted
04-21-2008, 06:04 AM
Use var_dump and dump the variable to see if it exists.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.