View Full Version : Plugin variables in template conditionals
toodimm
04-14-2011, 07:18 PM
Hi
Any chance that someone could tell me how to declare a variable in a plugin, such that that variable can then be used in a template conditional?
Thanks
Lynne
04-14-2011, 09:06 PM
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)
toodimm
04-14-2011, 09:28 PM
I really appreciate the link Lynne, but it doesn't actually answer the question I asked, which was:
how to declare a variable in a plugin, such that that variable can then be used in a template conditional
As far as I can tell, variables registered with the $templater->register method are only available in templates using {vb:raw my_var}, not in the template conditionals, such as <vb:if condition="$my_plugin_variable == 0">
Any other advise would be gratefully recieved.
Lynne
04-14-2011, 10:44 PM
Yeah, you need to preregister the variable for use in the template and then you are allowed to use it in the template conditional. It's the same variable that can be either spit out directly or used in a condition.
toodimm
04-14-2011, 11:53 PM
Thank you so much! Did the trick.
Sorry if my previous answer was a little rude, my only excuse is that I'm going nuts because of the complete lack of decent developer documentation available for vbulletin. If I could find the answers out myself, then I wouldn't be bothering the board!
BlueChipEarth
08-27-2013, 04:50 PM
I have a variable in the "style_fetch" hook called $mobile (for the mobile device detection mod, unimportant for this question, but that's why it's in "style_fetch").
I cannot, for the life of me, preRegister this variable for the "footer" template!!!
Why does this not work!?
in "style_fetch":
$TEST = "TESTING";
vB_Template::preRegister('footer', array('TEST' => $TEST));
in "footer" template:
{vb:raw TEST}
^Doesn't work.
Nor does the variable work in a conditional. What am I doing wrong!? Why does this have to be so complicated and confusing? I haven't seen even one post where someone preRegisters a variable and then uses it in a conditional, either... would the variable above be $TEST?
I'm so frustrated.
Zachery
08-27-2013, 04:59 PM
Did you try template_register_var ?
nhawk
08-27-2013, 05:03 PM
Your code, exactly as you have it posted works for me.
BlueChipEarth
08-27-2013, 05:37 PM
nhawk, I can't figure out why mine won't work... let me review the code and make sure I'm not in some parent if statement or something...
In the meantime, can you tell me what the registered variable would look like, or how to use it, in a conditional? Would it just be "$TEST" like:
<vb:if condition="$TEST == 'TESTING'">
something
</vb:if>
--------------- Added 1377629193 at 1377629193 ---------------
Hmm... seems I had a logic error in my code. I'm working within a mod, as I said earlier, and I think that since I clicked "desktop version" it was completely skipping mobile detection and, therefore, not setting the variable I was trying to access. D'oh!!!
I'm too tired today for programming... lousy downstairs neighbor found 12:30 AM a perfectly acceptable time to practice his guitar. He needs practice, to be sure... but let's do it at normal human hours, eh, Rock'n'Roll Dracula!?
nhawk
08-27-2013, 05:54 PM
The conditional the way you have it is correct.
BlueChipEarth
08-27-2013, 06:59 PM
nhawk, thanks a lot for your attention to this, and for taking your time to test my code! I appreciate it greatly!
After many trials and much tribulation, I've finally gotten my script to do what I wanted (really, amended a mod to work how I wanted).
Thanks again!
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.