PDA

View Full Version : looking for tutorial that explains how to show variable to different templates


radiofranky
08-08-2011, 08:26 PM
Hi,
I was wondering if there is something that explains how to show variables in not registered templates?

for example,

Variable X is registered in xyz template. How to modify it to show variable X in abc template.
how to show total threads in HEADER template?

thanks

kh99
08-09-2011, 01:53 PM
how to show total threads in HEADER template?


The variable $totalthreads is registered in the FORUMHOME template to show that value. But registering that variable in the header template won't work because the value is only calculated in the forum.php script, so if you are on any other page it will be blank. The only way to do it would be to create a plugin that calculates the value, and use a hook that will calculate it on every page.

Edit: Your plugin code could be something like (and parse_templates is probably a good hook to use):

$tt = $vbulletin->db->query_first("SELECT SUM(threadcount) AS totalthreads FROM " . TABLE_PREFIX . "forum");
$total = $tt['totalthreads'];

vB_Template::preRegister('header', array('totalthreads' => $total));

unset($tt, $total);


and then put {vb:raw totalthreads} in header template where you want it.

This adds a query to every page, and it's always good to reduce the number of queries to speed up your site, but it's probably not a big deal. You could cache the value in the datastore, but that's a more advanced topic.

Artashes
09-01-2011, 09:03 PM
is there a benefit of unsetting the $tt variable at the end? First time I've seen it used in this context…

kh99
09-01-2011, 09:27 PM
is there a benefit of unsetting the $tt variable at the end? First time I've seen it used in this context?

Probably not much, I guess I was feeling kind of "careful" when I wrote that. It's possible that other code executed after this could use $tt or $total and expect it to be unset to start with.

Lynne
09-01-2011, 11:12 PM
Cellarius wrote a really good article that you may be interested in - [vB4] Rendering templates and registering variables - a short guide (https://vborg.vbsupport.ru/showthread.php?t=228078)