View Full Version : How to call a variable from within the Threadbit template?
Tamtams
12-30-2009, 09:37 PM
I'm trying to convert over a vb3 product to vb4, and am running into an issue with the template system. Specifically, I am having trouble passing a variable to be outputted from within the template.
I am using the following code in a plugin which has the hook location of threadbit_display:
$templater = vB_Template::create('custom_template');
$templater->register('my_var', $my_var);
$templatevalues['custom_template'] = $templater->render();
vB_Template::preRegister('threadbit', $templatevalues);
Then in the style manager for threadbit, I added the following :
{vb:raw custom_template}
Unfortunately, it didn't work. Also tried {vb:raw custom_template.my_var} and it didn't work.
I've turned on dev mode, and custom_template appears under the list of template usage. It is highlighted in red though - I don't know what that means.
Anyone have any ideas?
Lynne
12-30-2009, 10:21 PM
$templater = vB_Template::create('custom_template');
$templater->register('my_var', $my_var);
$custom_template = $templater->render();
vB_Template::preRegister('threadbit', array('custom_template' => $custom_template));
Now you can use {vb:raw custom_template}
Tamtams
12-31-2009, 03:17 AM
Thanks Lynne - I copied and pasted that code exactly though, and it still didn't work... I changed $my_var to "Test123" just to make sure it was a variable assignment error or anything, but still a no go.
It is ok to be including this code in a plugin which uses hook location threadbit_display, correct?
Lynne
12-31-2009, 02:46 PM
Do you know if it is your template that isn't rendering or the preregistering that isn't working? Do some debugging in that area to find out (just assign xxx to a variable and preregister is and see if it shows up).
Tamtams
12-31-2009, 05:06 PM
Hi Lynne - That is exactly what I did - I assigned the value "Test123" to the variable, so it looks like it is the pre-registering which isn't working...
For example -
$templater = vB_Template::create('custom_template');
$templater->register('my_var', 'Test123');
$custom_template = $templater->render();
vB_Template::preRegister('threadbit', array('custom_template' => $custom_template));
{vb:raw custom_template} does not display "Test123" :(
Lynne
12-31-2009, 05:53 PM
This works just fine for me, so preregistering is working:
$custom_template = 'hello';
vB_Template::preRegister('threadbit', array('custom_template' => $custom_template));
Tamtams
12-31-2009, 07:14 PM
I'm a little mixed up - can I just use these two lines then, and insert whatever variable I want for $custom_template? I.e. is the 4 line version redundant, or do they both versions do the same thing?
Lynne
12-31-2009, 07:48 PM
You could just do it with two lines. I thought you had a template you were wanting to use though.
Tamtams
12-31-2009, 08:19 PM
Well, I think I should be using templates in order to use the cache system, correct? I.e. if I just use pre-register no caching will occur, right?
--------------- Added 1262298647 at 1262298647 ---------------
Also - just to clarify - i am able to access the variable using the 2 line version, but when I register as a template I am unable to access the variable.
When I turn on debug mode I can confirm the template is being loaded though. There is a (20) beside it, and its name is highlighted in red (which I assume means its not cached?)
Lynne
12-31-2009, 11:09 PM
I would guess something is wrong with your template then. Try just putting "hello" in the template and see if you get that to print out.
Tamtams
12-31-2009, 11:31 PM
I am using the default VBulletin 4, without any template changes.
I tried putting the word "hello" in the template, with no results - that's what caused me to start this thread in the first place...
Have you been able to successfully create and use a template in threadbit, or just via preprocess?
consolegaming
01-01-2010, 12:06 AM
I'd suggest trying a different hook like the parse_templates hook and see if that makes any difference. I had problems when I was trying to pre-register a custom template in the global hook. Changed it to the parse_templates hook after reading a comment and it fixed my problem. Can't hurt trying it anyway lol.
Tamtams
01-01-2010, 04:31 PM
I thought the problem with using hook parse_templates is the template will be loaded for every page load, even though it is only required for threadbit?
Either way, the problem has been narrowed down to registering the variables within the template - obviously creating the template is working, as it is appearing in the debug information, but registering it is not. Will try to move the register to another hook, and report back on what happens :)
--------------- Added 1262373019 at 1262373019 ---------------
Ok, I can't move the template to parse_templates, as it needs access to a function which isn't loaded by that hook. :(
Lynne
01-01-2010, 05:13 PM
Can we see the exact code you are currently using for the plugin (which location?) and the templates.
Tamtams
01-01-2010, 05:14 PM
I would guess something is wrong with your template then. Try just putting "hello" in the template and see if you get that to print out.
I tried this, no go. Very strange that the template can't render, even though VBulletin is recognizing that it was created!
consolegaming
01-01-2010, 09:12 PM
Ok, I can't move the template to parse_templates, as it needs access to a function which isn't loaded by that hook. :(
Hmm. I just did a quick test and it worked fine for me. I created a blank template called custom_template and I put "Testing123" into it. I then created a plugin with the code that Lynne originally suggested and finally added {vb:raw custom_template} to the threadbit Template. That worked fine for me and I ended up with loads of Testing123's appearing above the list of threads (because of the floated divs I presume).
Tamtams
01-02-2010, 07:56 PM
Here's the exact code i'm using - hook location is threadbit_display
$test_value = "It Works!";
$templater = vB_Template::create('threadworks');
$templater->register('custom_template', $test_value);
$custom_template = $templater->render();
vB_Template::preRegister('threadbit', array('threadworks' => $custom_template));
(I'm trying to port the Threadworks mod to VB4 )
Lynne
01-02-2010, 08:31 PM
What does your template threadworks look like?
consolegaming
01-02-2010, 09:10 PM
That is still working for me Tamtams. I copied that code directly into a new plugin that I created especially to test this. I then created a custom template called threadworks and then added {vb:raw threadworks} to the threadbit template.
Are you definitely adding the above variable (vb:raw thing) into the correct template i.e. threadbit. If you're trying to use it in one of the other templates then that'll be why. You need to pre-register it exactly for the template you're trying to use it in.
Granted my threadworks template that I created just has Testing123 in there but its working as I'd expect. Remember this will be showing in the thread listing (inside a forum) and not inside the thread itself (I doubt you're making that mistake but just mentioning the obvious lol).
As far as I can see either you're doing something different to the above or something is seriously going crazy on your forums lol. Especially if you're trying just a test message in your template. If the test message works then that'll obviously point to the template itself causing the issue but if not then I don't know what else to suggest.
EDIT: You haven't been trying to use {vb:raw custom_template} with the above plugin code have you?
If so then that'd be wrong lol. The pugin code above registers the $custom_template variable for use inside the threadworks template and the threadworks variable for use inside the threadbit template. So {vb:raw custom_template} with your plugin code will only show it's contents at the moment inside the threadworks template of yours.
Tamtams
01-03-2010, 01:01 PM
That is still working for me Tamtams. I copied that code directly into a new plugin that I created especially to test this. I then created a custom template called threadworks and then added {vb:raw threadworks} to the threadbit template.
Are you definitely adding the above variable (vb:raw thing) into the correct template i.e. threadbit. If you're trying to use it in one of the other templates then that'll be why. You need to pre-register it exactly for the template you're trying to use it in.
Granted my threadworks template that I created just has Testing123 in there but its working as I'd expect. Remember this will be showing in the thread listing (inside a forum) and not inside the thread itself (I doubt you're making that mistake but just mentioning the obvious lol).
As far as I can see either you're doing something different to the above or something is seriously going crazy on your forums lol. Especially if you're trying just a test message in your template. If the test message works then that'll obviously point to the template itself causing the issue but if not then I don't know what else to suggest.
EDIT: You haven't been trying to use {vb:raw custom_template} with the above plugin code have you?
If so then that'd be wrong lol. The pugin code above registers the $custom_template variable for use inside the threadworks template and the threadworks variable for use inside the threadbit template. So {vb:raw custom_template} with your plugin code will only show it's contents at the moment inside the threadworks template of yours.
Hah nope, I wasn't using {vb:raw custom_template }, I am using {vb:raw threadworks) . This issue has ben racking my head though! Consolegaming - could you just copy and paste the exact code your using so I can ensure I have replicated it properly on my local install! That would likely help me quickly identify why it is not working...
Thanks!
consolegaming
01-03-2010, 01:35 PM
Hah nope, I wasn't using {vb:raw custom_template }, I am using {vb:raw threadworks) . This issue has ben racking my head though! Consolegaming - could you just copy and paste the exact code your using so I can ensure I have replicated it properly on my local install! That would likely help me quickly identify why it is not working...
Thanks!
The code for the plugin?
I just copied and pasted the code from your own post here:
https://vborg.vbsupport.ru/showpost.php?p=1945283&postcount=17
I didn't add or remove anything from the plugin text lol.
You have activated your plugin haven't you? :D. Also it's probably just a typing error in the actual post but when you said what variable you used in the template you used a ) and not a }. I've highlighted it in red so you can see exactly what I'm talking about but I presume you typed it here rather than copying and pasting.
Tamtams
01-03-2010, 03:04 PM
OK - I FINALLY FIGURED IT OUT! :D
It was a stupid mistake of course - on my end - basically, I didn't know you actually had to creat a custom template through the style manager - I thought you could create custom templates pragmatically - i.e. just through PHP code.
For people who come find this thread later on, here's what needs to be done to get this working:
1) Create a new plugin under hook "threadbit_display" with the following code:
$test_value = "It Works!";
$templater = vB_Template::create('custom_template');
$templater->register('output', $test_value);
$custom_template= $templater->render();
vB_Template::preRegister('threadbit', array('custom_template' => $custom_template));
2) Now in the Style Manager, create a custom template called "custom_template", and place the following code in it:
{vb:raw output}
3) Now in the Style Manager, edit the template called "Threadbit" and add the following line:
{vb:raw custom_template}
Thanks everyone for your help :)
consolegaming
01-03-2010, 05:18 PM
Ahh lol. Nah the plugin code creates an instance of that template. It can't generate it out of nowhere. The template itself still needs to be setup in the Style Manager. The idea imo is to only use the plugin to setup the variables that you'll need to use in the template i.e. if most of the template is going to be html put it straight into the template rather than setting up a variable in the plugin with the html.
Nice to hear you got it working though.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.