The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
Understanding how to preregister variables.
Hi there,
I'm trying to understand how to preregister varaibles. In this example, I'm using FORUMHOME. This is my plugin: And this is my template: Nothing is being displayed. Why? Another question. I have read this tutorial, indeed: https://vborg.vbsupport.ru/showthread.php?t=228078 But I don't understand wht I need to use vB_Template::create to register a variable in a existing template. Does vB_Template::create create a new template and will I find it in my templates list? Is there any way to register a variable without creating and rendering a template? Thanks. |
#2
|
|||
|
|||
There are two ways to register variables: you can either register them after the template is created but before render() is called (as is done with my_str and my_arr in your example), or you can use preRegister(), which has to be done before the template is rendered. I think the reason for preRegister is mostly because without it, there would need to be a hook location everywhere a template is rendered, or else you couldn't add variables to the existing vb templates.
So the reason your code doesn't work is because you have the call to preRegister() after the template is rendered. But since you are creating and rendering your own custom template, you should just use a call to register. BTW, I should have explained this better when answering in your other thread, so sorry about that. |
#3
|
|||
|
|||
kh99, could you givfe me an example about the second way, please?
I have a question about my example. I don't want to render my own template, I want to use the FORUMHOME template. So, could I skip and omit the vBTemplate::create and the render function? If so, I understood that I only would need to call preRegister(), would it be enough? For example, Code:
$my_attr = array( 'apple' => 'red', 'banana' => 'yellow' ); vB_Template::preRegister('FORUMHOME' => $my_attr) |
#4
|
|||
|
|||
Quote:
Code:
vB_Template::preRegister('FORUMHOME', $my_attr) (it has a comma instead of =>). Then in your template you'd use {vb:raw red} and {vb:raw yellow}. It *is* possible to register an array then use loop tags in the template, but I'm going to assume that's not what you're trying to do, unless you say differently. Is that the example you wanted? If you're not rendering your own template then you don't need to (and in fact you can't) use register(). |
#5
|
|||
|
|||
Quote:
Code:
<vb:each from="my_attr" value="entry"> {vb:raw entry} </vb:each> Okay. there must be something wrong. I have edited {vb:raw header}and added somwhere: {vb:raw yellow}a Notice that there is a letter a. I have hooked the plugin to global_start, then I turned on the debug mode and I checked that global_start is executed. My plugin code is: Code:
$my_attr = array( 'apple' => 'red', 'banana' => 'yellow' ); vB_Template::preRegister('FORUMHOME', $my_attr); vB_Template::preRegister('header', $my_attr); What am I doing wrong? |
#6
|
|||
|
|||
The vb:each looks ok. But I.m not sure because I have to look it up every time. Of course if you wanted to do that you'd have to register the array as my_attr, which is a bit different than registering separate vars.
If you want to add something to header, headinclude, or footer (and a couple others I can't remember), you need a plugin using hook parse_templates. The hook code is called from inside a function, so you need to use a "global" statement for any globals you need. |
#7
|
|||
|
|||
There were not luck for the plugin. Despite it is hooked to parse_templates, it still does not show anything... this is crazy, uh?
--------------- Added [DATE]1373223942[/DATE] at [TIME]1373223942[/TIME] --------------- Quote:
It works now! |
#8
|
|||
|
|||
Oops, that was careless of me. Glad you figured it out.
|
#9
|
|||
|
|||
lol, I would have made the same mistake. When you write this stuff quickly to reply to a message and not look up the syntax for vbulletin .. well lets say I always get it slightly wrong.
|
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|