vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Understanding how to preregister variables. (https://vborg.vbsupport.ru/showthread.php?t=299893)

Seductor 07-07-2013 11:32 AM

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:
https://vborg.vbsupport.ru/external/2013/07/60.png

And this is my template:
https://vborg.vbsupport.ru/external/2013/07/61.png

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.

kh99 07-07-2013 01:12 PM

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.

Seductor 07-07-2013 02:44 PM

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)

And then could I access it from FORUMHOME using {vb:raw my_attr}?

kh99 07-07-2013 03:29 PM

Quote:

Originally Posted by Seductor (Post 2432574)
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)

And then could I access it from FORUMHOME using {vb:raw my_attr}?

Almost. First, the preRegister line should be like this:
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().

Seductor 07-07-2013 04:05 PM

Quote:

Originally Posted by kh99 (Post 2432577)
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.

I don't want to do it, but in that case, could I do...?

Code:

<vb:each from="my_attr" value="entry">
{vb:raw entry}
</vb:each>

--------------- Added [DATE]1373218964[/DATE] at [TIME]1373218964[/TIME] ---------------

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);

Why is not showing anything? It is showing only the letter a, as If {vb:raw yellow} is void.

What am I doing wrong?

kh99 07-07-2013 05:19 PM

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.

Seductor 07-07-2013 05:49 PM

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:

Originally Posted by kh99 (Post 2432577)
Then in your template you'd use {vb:raw red} and {vb:raw yellow}. ).

Finally I discovered why. It is not {vb:raw red}, but {cv:raw apple}. It is 'key' => 'value'.

It works now! :)

kh99 07-07-2013 06:10 PM

Oops, that was careless of me. Glad you figured it out.

tbworld 07-07-2013 08:58 PM

Quote:

Originally Posted by kh99 (Post 2432596)
Oops, that was careless of me. Glad you figured it out.

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.


All times are GMT. The time now is 12:07 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01293 seconds
  • Memory Usage 1,737KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (5)bbcode_code_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete