vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=251)
-   -   New Templates and registering variables (https://vborg.vbsupport.ru/showthread.php?t=279334)

clubvr4 02-29-2012 07:22 AM

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)

kh99 02-29-2012 11:36 AM

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 (Post 2304560)
... 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.

clubvr4 02-29-2012 12:28 PM

Quote:

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

kh99 02-29-2012 12:42 PM

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


clubvr4 03-01-2012 01:43 PM

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::preRegister('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 (Post 2305047)
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?

kh99 03-01-2012 02:02 PM

Quote:

Originally Posted by clubvr4 (Post 2305047)
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.

clubvr4 03-01-2012 03:39 PM

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?

clubvr4 03-06-2012 07:01 PM

Hiya, did you get a chance to test this?

kh99 03-06-2012 07:03 PM

No, of course I forgot all about it, sorry. I'll take a look at it now.

kh99 03-06-2012 07:50 PM

1 Attachment(s)
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


All times are GMT. The time now is 02:00 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.01864 seconds
  • Memory Usage 1,820KB
  • 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
  • (2)bbcode_code_printable
  • (9)bbcode_php_printable
  • (11)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete