PDA

View Full Version : Adding a site-wide template variable.


neverstop
10-13-2010, 12:17 AM
Hi,

I am looking to add a template variable that will be usable in all templates. The variable will be an integer between 1-9 that will be random on each page load. I want to use this for site wide "advert page-skinning" for lack of a better term.

Basically i want to be able to add something like this to any template:

<vb:if condition="$integer == 3">fu</vb:if>

or

<vb:if condition="$integer > 5">fu</vb:if>

or

<vb:if condition="in_array($integer, array(4,8,9))">fu</vb:if>

Something like this should be fairly simple via a plugin no?

Thanks in advance.

Lynne
10-13-2010, 03:44 AM
Used in *all* templates? So, in the header, and the footer, and the SHOWTHREAD, and ..... In vB4, you need to preregister variables for use in templates, so this may be problematic. You may try piggy-backing on the $bbuserinfo array, but I have no idea if that would work or not.

neverstop
10-13-2010, 03:57 AM
What got me thinking was that THIS_SCRIPT can be used in all templates cant it? of course I could be wrong

Lynne
10-13-2010, 01:58 PM
Yes. There are some variables that do not need to be registered like the $bbuserinfo variable I mentioned (along with $session and $vboptions, and I can't remember them all).

neverstop
10-21-2010, 01:55 AM
I will post my solution for posterity's sake.

I wasnt able to use a variable for all templates but registering vars for templates is pretty easy.

I added a plugin to the "parse_templates" hook location:

$ad_int = mt_rand(1, 10);
vB_Template::preRegister('header',array('ad_int' => $ad_int));
vB_Template::preRegister('navbar',array('ad_int' => $ad_int));
vB_Template::preRegister('adv_portal_cube_banner', array('ad_int' => $ad_int));
vB_Template::preRegister('postbit_legacy',array('a d_int' => $ad_int));

mt_rand() is a built in RNG and in this case 1 is the minimum, and 10 is the maximum.

I am not sure if this is the proper way to do this but it is working for me, I can use {vb:raw ad_int} in the specified templates. Maybe someone can chime in if there is a better way to do it.

Cheers