vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   How to pass a variable to template? (https://vborg.vbsupport.ru/showthread.php?t=275859)

L1teHawk 12-23-2011 01:16 AM

How to pass a variable to template?
 
Hi everyone,

I'm trying to pass a variable to the template "memberaction_dropdown". I created a new plugin with a hook to postbit_display_start (isn't that where the template is rendered?) with the following code:

PHP Code:

global $vbulletin;
$memname $vbulletin->db->query_first("SELECT * FROM " TABLE_PREFIX "mainapptable WHERE userid = '" $vbulletin->userinfo['userid'] . "'");
vB_Template::preRegister('memberaction_dropdown',array('memname' => $memname)); 

Then in my template, I did this:
{vb:raw memname.appname}

It does not work. Then I checked to see if $memname was set using a vb "if" conditional statement and it's not set. Can someone help me by pointing me in the right direction? Thank you very much!

kh99 12-23-2011 01:44 AM

You need to either add a "global $vbulletin;" line to the start of your code, or else use $this->registry->db instead. Also, if you want the database row you're reading to be for the user who wrote the post, you want to use $post['userid'] instead of $vbulletin->userinfo['userid'].

L1teHawk 12-23-2011 02:36 AM

Wow, thanks for the lightning fast response!

Ok, I did this:

PHP Code:

// global at the top
global $vbulletin;

// now I can use $vbulletin
$memname $vbulletin->db->query_first("SELECT * FROM "
                 
TABLE_PREFIX "mainapptable WHERE userid = '"
                  
$post['userinfo']['userid'] . "'"); 

// register the variable
vB_Template::preRegister('memberaction_dropdown',
                                          array(
'memname' => $memname)); 

And in my template:
<vb:if condition="$memname['app']">Found app!</vb:if>

Which doesn't work. I also tried {vb:raw memname.app} which didn't work either. Why is it so hard to pass data to a template :(

Edit: postbit_display_start is the correct hook right?

kh99 12-23-2011 11:06 AM

Oh, sorry about that - it should be $post['userid'] (I fixed it above, for future reference).

L1teHawk 12-23-2011 01:42 PM

Great! It works, thank you very much for your help. Enjoy a great holiday season :)

Edit: What would be the correct hook for forumhome_lastpostby? Thanks. I tried forumhome_start, forumdisplay_start, and several others :|

Edit 2: Figured it out. There is no correct hook for forumhome_lastpostby, so for those of you scouring the web for it, here's what I did: Search for the text "forumhome_lastpostby" and you'll come across a PHP file with some $templater registering the template. Above that code, put the custom hook, and then you'll be able to pass a variable to forumhome_lastpostby. Hope that helps someone :)


All times are GMT. The time now is 05:29 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.01151 seconds
  • Memory Usage 1,725KB
  • 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
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)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