PDA

View Full Version : How to pass a variable to template?


L1teHawk
12-23-2011, 01:16 AM
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:


global $vbulletin;
$memname = $vbulletin->db->query_first("SELECT * FROM " . TABLE_PREFIX . "mainapptable WHERE userid = '" . $vbulletin->userinfo['userid'] . "'");
vB_Template::preRegister('memberaction_dropdown',a rray('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:


// 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 :)