vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   How can I use a variable inside a php file in a custom template? (https://vborg.vbsupport.ru/showthread.php?t=268387)

EquinoxWorld 08-11-2011 06:37 PM

How can I use a variable inside a php file in a custom template?
 
Hello everyone, I have successfully used a function from a file into a custom template before using this plugin: (thanks to kh99 :) )

PHP Code:

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',array('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.

kh99 08-11-2011 06:48 PM

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 :

PHP Code:

$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 ??

kh99 08-11-2011 10:22 PM

I think what you have should work, but the PreRegister calls aren't quite right, you need a => between the name and the variable:

PHP Code:

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

Code:

global $cotw_sotw_sidemenu_vote, $cotw_sotw_sidemenu_nom;

in your function code.

EquinoxWorld 08-12-2011 01:49 AM

Quote:

Originally Posted by kh99 (Post 2232428)
I think what you have should work, but the PreRegister calls aren't quite right, you need a => between the name and the variable:

PHP Code:

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

Code:

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.

PHP Code:

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.

kh99 08-12-2011 12:31 PM

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

<?php

echo "hello world!!";


?>

Made this plugin:

PHP Code:

ob_start();
  require_once(
'test.php');
  
$php_include ob_get_contents();
ob_end_clean();
vB_Template::preRegister('COTW_SIDEMENU',array('php_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.

kh99 08-12-2011 05:29 PM

What hook locations are you using?

EquinoxWorld 08-12-2011 05:38 PM

Quote:

Originally Posted by kh99 (Post 2232711)
What hook locations are you using?

global_start.

--------------- Added [DATE]1313174864[/DATE] at [TIME]1313174864[/TIME] ---------------

And this is the plguin I am using to render the sidemenu .

PHP Code:

$templater vB_Template::create('COTW_SIDEMENU');
$cotw_sidemenu $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_sidemenu' => $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

Quote:

Originally Posted by EquinoxWorld (Post 2232712)
global_start.

--------------- Added [DATE]1313174864[/DATE] at [TIME]1313174864[/TIME] ---------------

And this is the plguin I am using to render the sidemenu .

PHP Code:

$templater vB_Template::create('COTW_SIDEMENU');
$cotw_sidemenu $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_sidemenu' => $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:

PHP Code:

$templater vB_Template::create('COTW_SIDEMENU');
$templater->register('php_include'$php_include);
$cotw_sidemenu $templater->render();
vB_Template::preRegister('COTW_SOTW',array('cotw_sidemenu' => $cotw_sidemenu)); 

And

Plugin Hook: parse_templates

Code:

ob_start();
  require_once('test.php');
  $php_include .= ob_get_contents();
ob_end_clean();



All times are GMT. The time now is 05:01 PM.

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.01249 seconds
  • Memory Usage 1,785KB
  • 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
  • (3)bbcode_code_printable
  • (10)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete