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

Reply
 
Thread Tools Display Modes
  #1  
Old 02-29-2012, 08:22 AM
clubvr4's Avatar
clubvr4 clubvr4 is offline
 
Join Date: Jul 2010
Posts: 116
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default New Templates and registering variables

Hi all,

I could really do with some guidance, I'm struggling to understand the whole process of registering variables in templates.

I have managed to get my head round creating new templates and presenting them via plugin within one of the default templates, for example.

Plugin.
Hook Location - Parse Templates.
Title - CVR4_Menu
Code -
PHP Code:
$templater vB_Template::create('CVR4_Menu');
$CVR4_Menu $templater->render();
vB_Template::preRegister('navbar', array('CVR4_Menu' => $CVR4_Menu)); 
I now call this from within navbar template using {vb:raw CVR4_Menu}

This works, i created a menu and this is showing - everything is cool here.

Now, what i want to achieve (for starters) is present the notifications to this template, i.e. the notifications will be added to the CVR4_Menu Template.

I followed several guides - threads i read include.
But, i don't think im getting it, yet... What I did was as follows.

Plugin.
Hook Location - Parse Templates.
Title - CVR4_Menu
Code -
PHP Code:
$templater vB_Template::create('CVR4_Menu');
$CVR4_Menu $templater->render();
vB_Template::preRegister('navbar', array('CVR4_Menu' => $CVR4_Menu));
vB_Template::preRegister('navbar', array('notifications_menubits' => $notifications_menubits)); 
This didn't work, so i then create a much simpler version...

Plugin.
Hook Location - Parse Templates.
Title - CVR4_Menu_preregister_notifications
Code -
PHP Code:
vB_Template::preRegister('CVR4_Menu', array('notifications_menubits' => $notifications_menubits)); 
This also didnt work, but i noticed after much fiddling that if i change the pre-register (in above example) to navbar and place the notifications code in the navbar it works, just like in header.

It seems to be that the CVR4_Menu template needs to be registered just like the navbar template before it will accept the notifications variables, but i really don't know where to start.

I read in one of the above threads that you can define some variables, like..

PHP Code:
/* Some Code, setting variables, (multidimensional) array */
$my_var "abc";
$my_array = array(
        
'key1' => 'value1',
        
'key2' => array(
                '
key21' => 'value21',
                '
key22' => 'value22'
        '
)
    );

/* render template and register variables */
$templater vB_Template::create('mytemplate');
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
$templater->render(); 
But i dont understand what the keys or values would/should be nor can i get my head around the above example.

Could someone please help me figure this out, i'm sure it will click for me soon but just now i'm just not getting it.

Maybe a working (simple) example of how to get notifications working in a custom template would aid me in my quest to understand.


Thanks for reading (all my waffle)
Reply With Quote
  #2  
Old 02-29-2012, 12:36 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The problem is that $notifications_menubits hasn't been set yet when your code runs (hook parse_templates). If you're using 4.1.10, there should be a hook location called process_templates_complete which you could try using instead.


Quote:
Originally Posted by clubvr4 View Post
... I read in one of the above threads that you can define some variables, like..

PHP Code:
/* Some Code, setting variables, (multidimensional) array */
$my_var "abc";
$my_array = array(
        
'key1' => 'value1',
        
'key2' => array(
                '
key21' => 'value21',
                '
key22' => 'value22'
        '
)
    );

/* render template and register variables */
$templater vB_Template::create('mytemplate');
    
$templater->register('my_var'$my_var);
    
$templater->register('my_array'$my_array);
$templater->render(); 
But i dont understand what the keys or values would/should be nor can i get my head around the above example.

The keys can be anything you want as long as they match what you use in the template. The values have to be variables that has a value at the time you call register() or preRegister(). I think a lot of people who are new to vb have problems because it sort of looks like there's a "language" with a set of values you can use wherever you want, but really the hook locations are specific places in the code, so the variables you have available in a plugin differ and depend on the hook location.
Reply With Quote
  #3  
Old 02-29-2012, 01:28 PM
clubvr4's Avatar
clubvr4 clubvr4 is offline
 
Join Date: Jul 2010
Posts: 116
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
The problem is that $notifications_menubits hasn't been set yet when your code runs (hook parse_templates). If you're using 4.1.10, there should be a hook location called process_templates_complete which you could try using instead.
Hi,

Thank you very much for responding, i'm desperate have this part of VB click for me - I can see many oppertunites and I visualise a much nicer UI for end users, just need to figure it out first

Am i looking at two seperate entities? one to register custom variables and another to render templates/conditionals? - if so, i'll overlook the variables for now and focus on the other.

Since original post i've moved code around and made another template, so ill use that now to prevent confusion..

Moved code from navbar to custom template called CVR4_searchprofile

Quote:
<vb:if condition="$notifications_total">
<li class="popupmenu notifications" id="notifications">
<a class="popupctrl" href="usercp.php{vb:raw session.sessionurl_q}">{vb:rawphrase your_notifications}: <span class="notifications-number"><strong>{vb:raw notifications_total}</strong></span></a>
<ul class="popupbody popuphover">
{vb:raw notifications_menubits}
</ul>
</li>
<vb:else />
<li class="popupmenu nonotifications" id="nonotifications">
<a class="popupctrl" href="usercp.php{vb:raw session.sessionurl_q}">{vb:rawphrase your_notifications}</a>
<ul class="popupbody popuphover">
<li>{vb:rawphrase no_new_messages}</li>
<li><a href="private.php{vb:raw session.sessionurl_q}">{vb:rawphrase inbox}</a></li>
</ul>
</li>
</vb:if>
So plug in is...

Plugin.
Hook Location - process_templates_complete
Title - CVR4_searchprofile
Code -
PHP Code:
$templater vB_Template::create('CVR4_searchprofile');
$CVR4_searchprofile $templater->render();
vB_Template::preRegister('navbar', array('CVR4_searchprofile' => $CVR4_searchprofile));
vB_Template::preRegister('CVR4_searchprofile', array('notifications_menubits' => $notifications_menubits));
  
$templater->render(); 
This didn't work, so returned it to default

PHP Code:
$templater vB_Template::create('CVR4_searchprofile');
$CVR4_searchprofile $templater->render();
vB_Template::preRegister('navbar', array('CVR4_searchprofile' => $CVR4_searchprofile));
$templater->render(); 
I then created a plug in on its own in an attempt to render the notifications in my custom template.

Plugin.
Hook Location - process_templates_complete
Title - CVR4_searchprofile_notification
Code -

PHP Code:
vB_Template::preRegister('CVR4_searchprofile', array('notifications_menubits' => $notifications_menubits));
$templater->render(); 
But nothing happened.

What do I need to do to make the notifications appear in my custom template? - i'm bamboozled.
Reply With Quote
  #4  
Old 02-29-2012, 01:42 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Variables you use in a template have to be registered (except some common ones are registered for you), and they have to be registered *before* the template is rendered). preRegister() is used mostly for the existing vb templates because usually there is no hook location to allow you to use register() when the template is being rendered .

So, when you're rendering your own template, call register() before the call to render(), like:

PHP Code:
$templater vB_Template::create('CVR4_searchprofile'); 
$templater->register('notifications_menubits'$notifications_menubits); 
$CVR4_searchprofile $templater->render(); 

// but use preRegister() here because you won't have a chance to call
// register() for the navbar template
vB_Template::preRegister('navbar', array('CVR4_searchprofile' => $CVR4_searchprofile)); 
Reply With Quote
  #5  
Old 03-01-2012, 02:43 PM
clubvr4's Avatar
clubvr4 clubvr4 is offline
 
Join Date: Jul 2010
Posts: 116
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi, thanks for that.

I tried copy pasting your code in but it didnt work - the notifications drop down isnt showing the number or notification info on the drop down.

Im still not sure on how this works, can i break this down they way i see if and determine how close/far of the mark i am?


1. This part grabes the code from the custom template i created.
Quote:
$templater = vB_Template::create('CVR4_searchprofile');
2. I presume this should be registering the menubits, based on? - classboot.php?
Quote:
$templater->register('notifications_menubits', $notifications_menubits);
3. renders the above?
Quote:
$CVR4_searchprofile = $templater->render();
4. This pre-registers the navbar template, but for what purpose? - I note that if i change the navbar to say header or footer, my template doesnt appear on the site.
Quote:
vB_Template:reRegister('navbar', array('CVR4_searchprofile' => $CVR4_searchprofile));
5. This part actually presents the code to the user?
Quote:
$templater->render();
I also followed this guide and tried moving the notifications code to the footer, but it didnt work either yet, the post suggest it work for the TheLastSuperman..

I've just upgraded to 4.1.11, has something changed to prevent this? - I checked the update notes but couldnt see anything obvious.

Thanks

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

Quote:
Originally Posted by clubvr4 View Post
4. This pre-registers the navbar template, but for what purpose? - I note that if i change the navbar to say header or footer, my template doesnt appear on the site.
Thinking abit, is it navbar because thats where the {vb:raw resides?
Reply With Quote
  #6  
Old 03-01-2012, 03:02 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by clubvr4 View Post
Thinking abit, is it navbar because thats where the {vb:raw resides?
Yes, that lets you use the string you created from your searchprofile template in the navbar template.


Quote:
5. This part actually presents the code to the user?

Actually that last $templater->render() doesn't do anything, I left it there by mistake. Sorry about that.

Are you still using hook process_templates_complete ? Just so you know, I haven't tried any of this code myself so I'm not sure why it's not working - maybe that hook won't work either. If I get a chance I'll try it later and see if I can figure out the problem.
Reply With Quote
  #7  
Old 03-01-2012, 04:39 PM
clubvr4's Avatar
clubvr4 clubvr4 is offline
 
Join Date: Jul 2010
Posts: 116
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'd appreciate that, I'm really not sure why its not working.

Regarding point 4, if I change navbar to say forumhome, then place the <vb:raw CVR4_searchprofile> to forumhome you'd expect that template to render in forumhome?

Does duplication of plugins have a negative effect?
Reply With Quote
  #8  
Old 03-06-2012, 08:01 PM
clubvr4's Avatar
clubvr4 clubvr4 is offline
 
Join Date: Jul 2010
Posts: 116
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hiya, did you get a chance to test this?
Reply With Quote
  #9  
Old 03-06-2012, 08:03 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

No, of course I forgot all about it, sorry. I'll take a look at it now.
Reply With Quote
  #10  
Old 03-06-2012, 08:50 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK, this works for me: I have a plugin using hook process_templates_complete with this code (should be same as posted above):

Code:
$templater = vB_Template::create('CVR4_searchprofile');  
$templater->register('notifications_menubits', $notifications_menubits);  
$CVR4_searchprofile = $templater->render();  

// but use preRegister() here because you won't have a chance to call 
// register() for the navbar template 
vB_Template::preRegister('navbar', array('CVR4_searchprofile' => $CVR4_searchprofile));

Then I created a new CVR4_searchprofile template like this:

Code:
<vb:if condition="$show[notifications]">
Show Notifications:
{vb:raw notifications_menubits}
<vb:else />
Don't Show Notifications
</vb:if>

and then I put {vb:raw CVR4_searchprofile} in my navbar template. And it looks like this:

Attachment 136887
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 07:36 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.05709 seconds
  • Memory Usage 2,336KB
  • Queries Executed 14 (?)
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
  • (2)bbcode_code
  • (9)bbcode_php
  • (11)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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_postinfo_query
  • fetch_postinfo
  • 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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete