vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   Fetching a custom template within a function (https://vborg.vbsupport.ru/showthread.php?t=176599)

inthezone 04-19-2008 04:38 AM

Fetching a custom template within a function
 
PHP Code:

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+tem plate" target="_blank">https://vborg.vbsupport.ru/showt...ustom+template</a>

That should help.

inthezone 04-19-2008 05:23 AM

Quote:

Originally Posted by nighthalk (Post 1494282)

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:
PHP Code:

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:
PHP Code:

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:

PHP Code:

// 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.


All times are GMT. The time now is 01:14 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01608 seconds
  • Memory Usage 1,740KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete