View Full Version : How to display pm notifications in a custom template?
Hilary
04-23-2016, 05:54 PM
Hi,
I've registered a custom template to include inside the header template with {vb:raw mytemplate}. It works... the
<vb:if condition="$show['member']">
condition inside mytemplate works, too. But
<vb:if condition="$notifications_total">doesn't.
So if I put this code in mytemplate -
<vb:if condition="$notifications_total"><div>there are notifications</div>
<vb:else /><div>no notifications here</div>
</vb:if>
I always see 'no notifications here'.
The exact same code in the header template works as expected.
Does anyone know a solution for this?
Lynne
04-23-2016, 05:57 PM
You will need to register the variable $notifications_total for use in your custom template.
Cellarius wrote a really good article that you may be interested in - [vB4] Rendering templates and registering variables - a short guide (https://vborg.vbsupport.ru/showthread.php?t=228078)
Hilary
04-23-2016, 07:24 PM
Hi Lynne! Thanks for the swift response.
I've just been reading through that thread, and I'm really glad to find you're still posting here.
I found this example code there:
$templater = vB_Template::create('memberbar_member_basic');
$memberbar_member_basic = $templater->render();
vB_Template::preRegister('navbar', array('notifications_menubits' => $notifications_menubits));
vB_Template::preRegister('navbar', array('memberbar_member_basic' => $memberbar_member_basic));
vB_Template::preRegister('navbar', array('notifications_menubits' => $notifications_menubits));
...looks like exactly what I want to do, displaying notifications in a custom template, though the op mentions it has a mistake and doesn't say what the mistake is (besides perhaps the duplicated line)...
So I took my existing plugin...
$templater = vB_Template::create('ubermenu');
$ubermenu = $templater->render();
vB_Template::preRegister('header', array('ubermenu' => $ubermenu));
and tried adding to it -
$templater = vB_Template::create('ubermenu');
$ubermenu = $templater->render();
vB_Template::preRegister('header', array('notifications_menubits' => $notifications_menubits));
vB_Template::preRegister('header', array('notifications_total' => $notifications_total));
vB_Template::preRegister('header', array('ubermenu' => $ubermenu));
No luck. Then thought this made no sense, since notifications already work in the header, it's the 'ubermenu' template that needs to know about them, so tried
vB_Template::preRegister('ubermenu', array('notifications_total' => $notifications_total));
etc instead, both in that plugin and in a separate one. Still no luck. :confused:
Help?
MarkFL
04-24-2016, 02:04 AM
First, you may combine the 3 lines:
vB_Template::preRegister('header', array('notifications_menubits' => $notifications_menubits));
vB_Template::preRegister('header', array('notifications_total' => $notifications_total));
vB_Template::preRegister('header', array('ubermenu' => $ubermenu));
as follows:
vB_Template::preRegister('header', array('notifications_menubits' => $notifications_menubits, 'notifications_total' => $notifications_total, 'ubermenu' => $ubermenu));
However, I am thinking this is more along the lines of what you want to do:
$templater = vB_Template::create('ubermenu');
$templater->register('notifications_menubits', $notifications_menubits);
$templater->register('notifications_total', $notifications_total);
$ubermenu = $templater->render();
vB_Template::preRegister('header', array('ubermenu' => $ubermenu));
This way you are registering the two variables your "ubermenu" template needs to render its output correctly, and then you can register that output (stored in $ubermenu) for use in the header template. :)
Hilary
04-24-2016, 06:54 AM
Thank you.
I'm sure you're right.
**EDIT**
Er, let me just delete everything I wrote before I found the stray ' in the code :o . I still haven't got it working, but I'll post a better update after I've tested a few more things.
--------------- Added 1461490554 at 1461490554 ---------------
Sorry to say that this -
$templater = vB_Template::create('ubermenu');
$templater->register('notifications_menubits', $notifications_menubits);
$templater->register('notifications_total', $notifications_total);
$ubermenu = $templater->render();
vB_Template::preRegister('header', array('ubermenu' => $ubermenu));
- definitely doesn’t work.
I replaced my whole ubermenu template with the original notifications code, and finally with
<div><vb:if condition="$notifications_total">There are notifications<vb:else />Yes, we have no notifications</vb:if>
<ul>{vb:raw notifications_menubits}</ul></div>
It always displays the 'no notifications' option (and I'm logged in, and have loads of notifications, which show up fine in the default skin), and doesn't display the notifications_menubits at all. IOW, the presence or absence of those two 'register' lines in the plugin makes no difference to the result.
MarkFL
04-24-2016, 10:04 AM
What hook location are you using for your plugin?
Hilary
04-24-2016, 10:18 AM
parse_templates
(I tried 'global_start' first, but the template didn't appear at all.)
MarkFL
04-24-2016, 10:22 AM
Try "global_setup_complete"...:)
Hilary
04-24-2016, 10:33 AM
Nope - template disappears altogether again.
MarkFL
04-24-2016, 06:45 PM
Okay, here's my workaround:
The "mytemplate" template:
<div>
<vb:if condition="$notifications_total">
there are notifications
<vb:else />
no notifications here
</vb:if>
</div>
The plugin, hooked at "process_templates_complete":
$templater = vB_Template::create('mytemplate');
$templater->register('notifications_menubits', $notifications_menubits);
$templater->register('notifications_total', $notifications_total);
$ubermenu = $templater->render();
$header = str_replace('{ubermenu}', $ubermenu, $header);
Now, in your "header" template, put the string "{ubermenu}" wherever you want the rendering of "mytemplate" to appear. :)
Hilary
04-25-2016, 08:06 AM
It's fantastically kind of you to put in all this work for me - I really appreciate it. And you've cracked it! The above code - substituting 'ubermenu' for 'mytemplate' in line 1 ;) - works just as intended.
MarkFL
04-25-2016, 10:22 AM
It's fantastically kind of you to put in all this work for me - I really appreciate it. And you've cracked it! The above code - substituting 'ubermenu' for 'mytemplate' in line 1 ;) - works just as intended.
From what I found, the "parse_templates" hook location is the only one that will work for the "header" template, but at that hook, the notifications data is not available yet. So, I had to find a hook location that is global, and inject data into the "header template another way. :)
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.