Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 08-11-2011, 06:37 PM
EquinoxWorld EquinoxWorld is offline
 
Join Date: Nov 2009
Location: Naples
Posts: 354
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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.
Reply With Quote
  #2  
Old 08-11-2011, 06:48 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

There's two ways you could do it (that I can think of): put a vB_Template:reRegister() call in cotw_sotw.php where you have the variable set, or make the variable global and add another call to vB_Template:reRegister() (where you have the existing one).
Reply With Quote
  #3  
Old 08-11-2011, 09:56 PM
EquinoxWorld EquinoxWorld is offline
 
Join Date: Nov 2009
Location: Naples
Posts: 354
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

Any ideas ??
Reply With Quote
  #4  
Old 08-11-2011, 10:22 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #5  
Old 08-12-2011, 01:49 AM
EquinoxWorld EquinoxWorld is offline
 
Join Date: Nov 2009
Location: Naples
Posts: 354
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
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.
Reply With Quote
  #6  
Old 08-12-2011, 12:31 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 08-12-2011, 05:07 PM
EquinoxWorld EquinoxWorld is offline
 
Join Date: Nov 2009
Location: Naples
Posts: 354
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

I bet it's something real simple but I just can't figure it out.
Reply With Quote
  #8  
Old 08-12-2011, 05:29 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What hook locations are you using?
Reply With Quote
  #9  
Old 08-12-2011, 05:38 PM
EquinoxWorld EquinoxWorld is offline
 
Join Date: Nov 2009
Location: Naples
Posts: 354
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
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)
Reply With Quote
  #10  
Old 08-12-2011, 06:02 PM
Badshah93 Badshah93 is offline
 
Join Date: Jun 2010
Location: India
Posts: 505
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by EquinoxWorld View Post
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();
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:41 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07972 seconds
  • Memory Usage 2,293KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (10)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete