View Full Version : How can I use a variable inside a php file in a custom template?
EquinoxWorld
08-11-2011, 06:37 PM
Hello everyone, I have successfully used a function from a file into a custom template before using this plugin: (thanks to kh99 :) )
ob_start();
require_once('intuitco/cotw/functions/cotw_func_print_nom.php');
cotw_sotw_print_nom(true);
$cotw_print_nominations = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('COTW_SOTW_NOMINATIONS',a rray('cotw_print_nominations' => $cotw_print_nominations));
Now I want to use a variable that I have defined in cotw_sotw.php in a template that is rendered into this page. Allow me explain further.
I have my page cotw_sotw.php which uses the template COTW_SOTW . Inside this template I have {vb:raw cotw_sidemenu} which is another custom template (COTW_SIDEMENU).
There is a variable in cotw_sotw.php file that I need to use(register) inside the sidemenu template (COTW_SIDEMENU). How would I go about doing so?? If even possible. :) Any information anyone might have will be appreciated. Thanks for your time everyone.
Best Regards.
There's two ways you could do it (that I can think of): put a vB_Template::preRegister() call in cotw_sotw.php where you have the variable set, or make the variable global and add another call to vB_Template::preRegister() (where you have the existing one).
EquinoxWorld
08-11-2011, 09:56 PM
Well here we go... :)
First thing I tried was calling to pre-register the variables within the page file (sotw_cotw.php).
Here is the php file snippe :
$result = $db->query_read("SELECT * FROM cotw_sotw_nominations");
if (mysql_num_rows($result) == $vbulletin->options['cotw_sotw_count']) {
$cotw_sotw_nom_or_vote = "Nominations are closed for this contest.<br/>
Click <a href=intuitco/cotw/cotw_sotw_vote.php>here</a> to start voting.";
$cotw_sotw_sidemenu_nom = "(Closed)";
$cotw_sotw_sidemenu_vote = "(Open)";
}
if (mysql_num_rows($result) < $vbulletin->options['cotw_sotw_count']) {
$cotw_sotw_nom_or_vote = "Nominations are open for this contest.<br/>
Click <a href=./intuitco/cotw/cotw_sotw_nominate.php>here</a> to start nominating.";
$cotw_sotw_sidemenu_nom = "(Open)";
$cotw_sotw_sidemenu_vote = "(Closed)";
}
vB_Template::preRegister('COTW_SIDEMENU', array('cotw_sotw_sidemenu_nom' , $cotw_sotw_sidemenu_nom));
vB_Template::preRegister('COTW_SIDEMENU', array('cotw_sotw_sidemenu_vote' , $cotw_sotw_sidemenu_vote));
The variables that I want to use in COTW_SIDEMENU are $cotw_sotw_sidemenu_nom and $cotw_sotw_sidemenu_vote . Using this method I was unable to get the results to display in the SIDEMENU template that's inside the page template COTW_SOTW . About the second option to be honest I have not dealt with globals a whole lot yet. If I am not mistaken (which I definitely could be) a global variable should be used within a function? I am not sure how to declare these variables globals using the code I have if it's not a function...:confused:
Any ideas ??
I think what you have should work, but the PreRegister calls aren't quite right, you need a => between the name and the variable:
vB_Template::preRegister('COTW_SIDEMENU', array('cotw_sotw_sidemenu_nom' => $cotw_sotw_sidemenu_nom));
vB_Template::preRegister('COTW_SIDEMENU', array('cotw_sotw_sidemenu_vote' => $cotw_sotw_sidemenu_vote));
And just for the record, to make the variables global you'd just have to put
global $cotw_sotw_sidemenu_vote, $cotw_sotw_sidemenu_nom;
in your function code.
EquinoxWorld
08-12-2011, 01:49 AM
I think what you have should work, but the PreRegister calls aren't quite right, you need a => between the name and the variable:
vB_Template::preRegister('COTW_SIDEMENU', array('cotw_sotw_sidemenu_nom' => $cotw_sotw_sidemenu_nom));
vB_Template::preRegister('COTW_SIDEMENU', array('cotw_sotw_sidemenu_vote' => $cotw_sotw_sidemenu_vote));
And just for the record, to make the variables global you'd just have to put
global $cotw_sotw_sidemenu_vote, $cotw_sotw_sidemenu_nom;
in your function code.
Thanks for your help kh99, although I haven't been able to get this to work yet. I tried with the same method as the function form the previous threads and using a plugin to call it to the sidebar template but it doesn't show in the cotw_sotw.php page. Could it be that since I am rendering the function in COTW_SIDEMENU template which in turn is being rendered in COTW_SOTW template, maybe I need to also register this function in the COTW_SOTW template as well?? This is the function I am using.
function cotw_sotw_print_side_vote_nom($dummy)
{
global $vbulletin;
$result = $vbulletin->db->query_read("SELECT * FROM cotw_sotw_nominations");
if (mysql_num_rows($result) == 7) {
echo "<li class=inactive><a href=intuitco/cotw/cotw_sotw_nominate.php>Nominations (<font color=red>Closed</font>)</a></li>
<li class=inactive><a href=intuitco/cotw/cotw_sotw_vote.php>Voting (<font color=lime>Open</font>)</a></li>";
}
if (mysql_num_rows($result) < 7) {
echo "<li class=inactive><a href=intuitco/cotw/cotw_sotw_nominate.php>Nominations (<font color=lime>Open</font>)</a></li>
<li class=inactive><a href=intuitco/cotw/cotw_sotw_vote.php>Voting (<font color=red>Closed</font>)</a></li>";
}
}
I know this method as we previously discussed works because I have another function showing up fine in another page, but in this case it is in a template that is also being rendered in another. I have tried all methods a few times each making sure everything was correct but no luck unfortunately....or maybe I am missing something very obvious. In any case, I do appreciate your help, if anything else comes up or something I might be missing please do share :) Thanks for your time.
Sorry, I can't see what might be wrong. You should only have to register the variables in the template once, it doesn't matter if that template is included in another template. You do, of course, have to register them before the template is rendered, but I can't tell if you're doing that from the code you posted.
EquinoxWorld
08-12-2011, 05:07 PM
OK I just ran a VERY easy test. I made this file ... (Does not get ANY simpler)
<?php
echo "hello world!!";
?>
Made this plugin:
ob_start();
require_once('test.php');
$php_include = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('COTW_SIDEMENU',array('ph p_include' => $php_include));
And added {vb:raw php_include} in my COTW_SIDEMENU template (which is being also rendered into all the other pages of my mod) but still nothing shows. I tried the same thing but in a regular template and it works fine so I'm guessing it has to be something to do with the fact that this template is being rendered into all the other templates, maybe an extra step is required??:confused:
I bet it's something real simple but I just can't figure it out.
What hook locations are you using?
EquinoxWorld
08-12-2011, 05:38 PM
What hook locations are you using?
global_start.
--------------- Added 1313174864 at 1313174864 ---------------
And this is the plguin I am using to render the sidemenu .
$templater = vB_Template::create('COTW_SIDEMENU');
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
Then I just add the variable of the sidemenu to every php page (in the php file before the render output) and the sidemenu shows on every page. (but no variables that I use within the sidemenu template)
Badshah93
08-12-2011, 06:02 PM
global_start.
--------------- Added 1313174864 at 1313174864 ---------------
And this is the plguin I am using to render the sidemenu .
$templater = vB_Template::create('COTW_SIDEMENU');
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
Then I just add the variable of the sidemenu to every php page (in the php file before the render output) and the sidemenu shows on every page. (but no variables that I use within the sidemenu template)
Try This:
$templater = vB_Template::create('COTW_SIDEMENU');
$templater->register('php_include', $php_include);
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
And
Plugin Hook: parse_templates
ob_start();
require_once('test.php');
$php_include .= ob_get_contents();
ob_end_clean();
global_start.
For both plugins? That could be the problem, because unless you set the execution order you probably don't know which one is running first. But if you're using the same hook for more than one plugin you could also combine them into one. (What Sherif posted above might also work because it's using two hook locations).
EquinoxWorld
08-12-2011, 06:21 PM
Try This:
$templater = vB_Template::create('COTW_SIDEMENU');
$templater->register('php_include', $php_include);
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
And
Plugin Hook: parse_templates
ob_start();
require_once('test.php');
$php_include .= ob_get_contents();
ob_end_clean();
Thanks for your reply Sherif, although still no results.
These are the plugins I am using.
hook parse_templates
$templater = vB_Template::create('COTW_SIDEMENU');
$templater->register('php_include', $php_include);
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
and
hook global_start
ob_start();
require_once('test.php');
$php_include .= ob_get_contents();
ob_end_clean();
Then adding {vb:raw php_include} to my COTW_SIDEMENU template. Unfortunately still no results. :(
Badshah93
08-12-2011, 06:41 PM
Thanks for your reply Sherif, although still no results.
These are the plugins I am using.
hook parse_templates
$templater = vB_Template::create('COTW_SIDEMENU');
$templater->register('php_include', $php_include);
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
and
hook global_start
ob_start();
require_once('test.php');
$php_include .= ob_get_contents();
ob_end_clean();
Then adding {vb:raw php_include} to my COTW_SIDEMENU template. Unfortunately still no results. :(
I told you to use
Parse template hook for this code
ob_start();
require_once('test.php');
$php_include .= ob_get_contents();
ob_end_clean();
the hook for other code is which u were using earlier.
EquinoxWorld
08-12-2011, 06:49 PM
I told you to use
Parse template hook for this code
ob_start();
require_once('test.php');
$php_include .= ob_get_contents();
ob_end_clean();
the hook for other code is which u were using earlier.
Sorry about that. Tried that as well but still no luck I'm afraid. The sidemenu shows perfectly but just not that variables output. Would you think it may have to do with the execution order ? Maybe the template is being rendered before the variable is included in the template? I'm so frustrated right now. :( In any case thanks for your help guys, I really appreciate it. If anyone has any more ideas or anything info at ALL please let me know. Thanks.
P.S.: If anyone is willing to help further I would gladly provide log in to my test site if anyone is up for it. To be honest I have reached my skill level on this one.
Badshah93
08-12-2011, 06:54 PM
Sorry about that. Tried that as well but still no luck I'm afraid. The sidemenu shows perfectly but just not that variables output. Would you think it may have to do with the execution order ? Maybe the template is being rendered before the variable is included in the template? I'm so frustrated right now. :( In any case thanks for your help guys, I really appreciate it. If anyone has any more ideas or anything info at ALL please let me know. Thanks.
Try This
Plugin Hook: parse_templates
ob_start();
require_once('test.php');
$php_include = ob_get_contents();
ob_end_clean();
$templater = vB_Template::create('COTW_SIDEMENU');
$templater->register('php_include', $php_include);
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
To be fair, why you are including external file from outside. just write php code in plugins or make functions.
EquinoxWorld
08-12-2011, 07:04 PM
Try This
Plugin Hook: parse_templates
ob_start();
require_once('test.php');
$php_include = ob_get_contents();
ob_end_clean();
$templater = vB_Template::create('COTW_SIDEMENU');
$templater->register('php_include', $php_include);
$cotw_sidemenu = $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
To be fair, why you are including external file from outside. just write php code in plugins or make functions.
HURRRAAYYYYYY!!!! FOR SHERRRIFFFFFF Needless to say I will NOT shoot the sherif :p
Thanks a bunch for your help. Also thanks to our advisor kh99, without your guys help I would have probably exploded my server or something. Thanks again guys. I'm so stoked this works now.
About your question I guess I have not been too familiar yet with vb plugin system as you can tell; as a noob still I feel more comfortable using files. I feel I have more control and organization with files than plugins. Performance wise does it have any impact choosing one over the other??
Badshah93
08-12-2011, 07:09 PM
HURRRAAYYYYYY!!!! FOR SHERRRIFFFFFF Needless to say I will NOT shoot the sherif :p
Thanks a bunch for your help. Also thanks to our advisor kh99, without your guys help I would have probably exploded my server or something. Thanks again guys. I'm so stoked this works now.
About your question I guess I have not been too familiar yet with vb plugin system as you can tell; as a noob still I feel more comfortable using files. I feel I have more control and organization with files than plugins. Performance wise does it have any impact choosing one over the other??
not really.. but when plugin has option to add php code then why not use it..
EquinoxWorld
08-12-2011, 07:19 PM
not really.. but when plugin has option to add php code then why not use it..
True. One last question. If I wanted to include the variable in more than one template can I do this:
vB_Template::preRegister('COTW_SOTW',array('cotw_s idemenu' => $cotw_sidemenu));
vB_Template::preRegister('other template',array('cotw_sidemenu' => $cotw_sidemenu));
vB_Template::preRegister('other template',array('cotw_sidemenu' => $cotw_sidemenu));
vB_Template::preRegister('other template',array('cotw_sidemenu' => $cotw_sidemenu));
Or is there a better way of doing it If it is for more than a couple of templates?
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.