Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-07-2013, 11:32 AM
Seductor Seductor is offline
 
Join Date: May 2011
Posts: 128
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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.
Reply With Quote
  #2  
Old 07-07-2013, 01:12 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #3  
Old 07-07-2013, 02:44 PM
Seductor Seductor is offline
 
Join Date: May 2011
Posts: 128
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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}?
Reply With Quote
  #4  
Old 07-07-2013, 03:29 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Seductor View Post
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().
Reply With Quote
  #5  
Old 07-07-2013, 04:05 PM
Seductor Seductor is offline
 
Join Date: May 2011
Posts: 128
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
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?
Reply With Quote
  #6  
Old 07-07-2013, 05:19 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #7  
Old 07-07-2013, 05:49 PM
Seductor Seductor is offline
 
Join Date: May 2011
Posts: 128
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 View Post
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!
Reply With Quote
  #8  
Old 07-07-2013, 06:10 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Oops, that was careless of me. Glad you figured it out.
Reply With Quote
  #9  
Old 07-07-2013, 08:58 PM
tbworld tbworld is offline
 
Join Date: Oct 2008
Posts: 2,126
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04434 seconds
  • Memory Usage 2,244KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_code
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete