The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
[HOW TO - vB4] Rendering templates and registering variables - a short guide
Introduction Starting with vB4, templates no longer get output using eval: PHP Code:
What's more: Variables and arrays from plugins that are executed on a page no longer can automatically be accessed in the templates of that page. They need to be registered first. . Basic functionality to render templates and register all variables/arrays you want to use inside PHP Code:
HTML Code:
{vb:raw my_var} {vb:raw my_array.key1} {vb:raw my_array.key2.key21} . . . Now, with the result of the rendering we can do several things: . Output template directly - custom pages PHP Code:
Note the second line, which is special for this type of use: PHP Code:
. Use a template hook PHP Code:
. Save into a variable for later use in custom template PHP Code:
PHP Code:
HTML Code:
{vb:raw my_template_rendered} . Save into an array and preregister to use in an existing/stock template PHP Code:
HTML Code:
{vb:raw my_insertvar} Essentially the same as what I put for preRegister would be the following two lines. They could replace the last two lines in the above php codebox: PHP Code:
. . Bonus track: ...whatever you do, cache your templates! Now you know how to get your templates on screen - once you succeeded in doing that, make sure to do it in a fast and ressource saving manner: make use of vB's template cache. To see whether your templates are cached or not, activate debug mode by adding $config['Misc']['debug'] = true;to your config.php (don't ever use that on your live site!). Among the debug info is a list of all templates called, and non-cached templates will show up in red. To cache your templates, add a plugin at hook cache_templates with the following code: PHP Code:
. Hope this helps! -cel ---- Addendum - There are now two blog posts on vb.com related to this topic: http://www.vbulletin.com/forum/entry...in-4-templates http://www.vbulletin.com/forum/entry...-4-based-files |
#252
|
|||
|
|||
It could be because I had the code wrong - I fixed it in my post above.
|
#253
|
|||
|
|||
Yay! that was it. I can't thank you enough.
|
#254
|
|||
|
|||
Quote:
|
#255
|
||||
|
||||
It has nothing at all to do with the topic of this article, but:
Last line: $randombanner = '<a href="'.$href.'" target="_blank"><img border="0" src="'.$src.'" /></a>'; |
#256
|
|||
|
|||
Yes, I know, apologies...and thank you
|
#257
|
|||
|
|||
The thing I am not understanding at this point is. If I am trying to preregister a variable for use with a stock template, in this case header. Where am I putting my code?
Is there a stock location to add this to or am I having to jump from adding a simple variable used on sites since vb 3.x into learning the basics of plugin writing? GLOBALS.permissions.pmquota isn't working for me in a new situation so I am trying to learn how to register permissions with the header template. I believe the code is going to look like this? Code:
$permissions = $vbulletin->userinfo['permissions'];
vB_Template::preRegister('header',array('permissions' => $permissions));
Also is the above enough or do I need to add more like: Code:
$permissions['pmquota'] = vb_number_format($vbulletin->userinfo['permissions']['pmquota']); |
#258
|
|||
|
|||
I answered in your other thread, but I'll answer more generally here: for the header, navbar, and footer templates I would recommend the hook parse_templates, since that's always executed just before those templates are rendered.
But the variable you want to use has to be available at that hook, and the timing of the call to the parse_templates hook can change. In the case of $permissions, that is set just after the global_start hook is called, so if some plugin using the global_start hook renders a template, parse_templates will be called but $permissions won't have been set yet so preRegistering it to a template won't work. (That's also why GLOBALS.permissions.pmquota stopped working - using preRegister() instead won't make any difference). But as I mentioned in the other thread, $permission is just a copy of $vbulletin->userinfo['permissions'], so if you use {vb:raw bbuserinfo.permissions.pmquota} in the template, that may work without any preRegistering. |
#259
|
|||
|
|||
is this possible
PHP Code:
|
#260
|
||||
|
||||
As you wrote it, it is not going to work, you can't just put an "or" in the middle of any code.
I'm not really sure what your goal is or I'd give you an idea where to go... Maybe.. PHP Code:
Assuming list1 always exists array[0] gets its value Then If list2 exists the next element in array (array[1] in this case) gets the value of list2 |
#261
|
|||
|
|||
Hi, i am trying to learn how to adjust the plugins for v3 to v4.
Can anybody guide me and tell me what it's wrong? Code working in 3.6.6. Code:
<?php // ####################### SET PHP ENVIRONMENT ########################### error_reporting(E_ALL & ~E_NOTICE); // #################### DEFINE IMPORTANT CONSTANTS ####################### define('THIS_SCRIPT', 'hangman'); $globaltemplates = array( 'hangman' ); // pre-cache templates used by specific actions $actiontemplates = array(); //vB Hangman 2.0 by Dr. Erwin Loh (Admin at vBulletin.org) //Enjoy this hack! require_once('./global.php'); $hangmanwords=&$vbulletin->options['hangmanwords']; $hangmanmax=&$vbulletin->options['hangmanmax']; $hangmanguests=&$vbulletin->options['hangmanguests']; if ($hangmanguests==0) { if (!$vbulletin->userinfo['userid']) { print_no_permission(); } } $vbulletin->input->clean_array_gpc('r', array( 'letters' => TYPE_STR, 'n' => TYPE_UINT )); $letters = &$vbulletin->GPC['letters']; $n = &$vbulletin->GPC['n']; $additional_letters = " -.,;!?%&0123456789"; $max=$hangmanmax; $hangmanwords = strtoupper($hangmanwords); $words = explode(",",$hangmanwords); $alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $all_letters=$letters.$additional_letters; $wrong = 0; srand((double)microtime()*1000000); if (!isset($n)) { $n = rand(1,count($words)-1); } $word_line=""; $word = $words[$n]; $done = 1; for ($x=0; $x < strlen($word); $x++) { if (strstr($all_letters, $word[$x])) { if ($word[$x]==" ") $word_line.=" "; else $word_line.=$word[$x]; } else { $word_line.="_<font size=1> </font>"; $done = 0; } } if (!$done) { for ($c=0; $c<26; $c++) { if (strstr($letters, $alpha[$c])) { if (strstr($words[$n], $alpha[$c])) { $links .= "\n<B>$alpha[$c]</B> "; } else { $links .= "\n<FONT color=\"red\">$alpha[$c] </font>"; $wrong++; } } else { $links .= "\n<A HREF=\"$PHP_SELF?letters=$alpha[$c]$letters&n=$n\">$alpha[$c]</A> "; } } $nwrong=$wrong; if ($wrong >= $max) { $n = rand (1,count($words)-1); if ($n>(count($words)-1)) $n=0; $sorry .= "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n<p><BR><FONT color=\"red\"><BIG>SORRY, YOU ARE HANGED!!!</BIG></FONT><BR><BR>"; if (strstr($word, " ")) $term="phrase"; else $term="word"; $play .= "The $term was \"<B>$word</B>\"<BR><BR>\n<A HREF=$PHP_SELF?n=$n>Play again.</A>\n\n"; } else { $guess .= " Number of Wrong Guesses Left: <B>".($max-$wrong)."</B><BR>\n<H1><font size=5>\n$word_line</font></H1>\n<P><BR>Choose a letter:<BR><BR>\n$links\n"; } } else { $n = rand (1,count($words)-1); if ($n>(count($words)-1)) $n=0; $win .= "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n<P><BR><BR><B>Congratulations $bbuserinfo[username]!!! You have won!!!</B><BR><BR><BR>\n<A HREF=$PHP_SELF?n=$n>Play again</A>\n\n"; } $navbits = construct_navbits(array('' => $vbphrase['vb_hangman'])); eval('$navbar = "' . fetch_template('navbar') . '";'); eval('print_output("' . fetch_template(hangman) . '");'); ?> Code:
<?php // ####################### SET PHP ENVIRONMENT ########################### error_reporting(E_ALL & ~E_NOTICE); ini_set("display_errors", 1); // #################### DEFINE IMPORTANT CONSTANTS ####################### define('THIS_SCRIPT', 'hangman'); // ################### PRE-CACHE TEMPLATES AND DATA ###################### // get special phrase groups $phrasegroups = array(); // get special data templates from the datastore $specialtemplates = array(); $globaltemplates = array( 'hangman' ); // pre-cache templates used by specific actions $actiontemplates = array(); //vB Hangman 2.0 by Dr. Erwin Loh (Admin at vBulletin.org) //Enjoy this hack! require_once('./global.php'); $hangmanwords=&$vbulletin->options['hangmanwords']; $hangmanmax=&$vbulletin->options['hangmanmax']; $hangmanguests=&$vbulletin->options['hangmanguests']; if ($hangmanguests==0) { if (!$vbulletin->userinfo['userid']) { print_no_permission(); } } $vbulletin->input->clean_array_gpc('r', array( 'letters' => TYPE_STR, 'n' => TYPE_UINT )); $letters = &$vbulletin->GPC['letters']; $n = &$vbulletin->GPC['n']; $additional_letters = " -.,;!?%&0123456789"; $max=$hangmanmax; $hangmanwords = strtoupper($hangmanwords); $words = explode(",",$hangmanwords); $alpha = "ABCDEFGHIJKLMN?OPQRSTUVWXYZ"; $all_letters=$letters.$additional_letters; $wrong = 0; srand((double)microtime()*1000000); if (!isset($n)) { $n = rand(1,count($words)-1); } $word_line=""; $word = $words[$n]; $done = 1; for ($x=0; $x < strlen($word); $x++) { if (strstr($all_letters, $word[$x])) { if ($word[$x]==" ") $word_line.=" "; else $word_line.=$word[$x]; } else { $word_line.="_<font size=1> </font>"; $done = 0; } } if (!$done) { for ($c=0; $c<26; $c++) { if (strstr($letters, $alpha[$c])) { if (strstr($words[$n], $alpha[$c])) { $links .= "\n<B>$alpha[$c]</B> "; } else { $links .= "\n<FONT color=\"red\">$alpha[$c] </font>"; $wrong++; } } else { $links .= "\n<A HREF=\"$PHP_SELF?letters=$alpha[$c]$letters&n=$n\">$alpha[$c]</A> "; } } $nwrong=$wrong; if ($wrong >= $max) { $n = rand (1,count($words)-1); if ($n>(count($words)-1)) $n=0; $sorry .= "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n<p><BR><FONT color=\"red\"><BIG>SORRY, YOU ARE HANGED!!!</BIG></FONT><BR><BR>"; if (strstr($word, " ")) $term="phrase"; else $term="word"; $play .= "The $term was \"<B>$word</B>\"<BR><BR>\n<A HREF=$PHP_SELF?n=$n>Play again.</A>\n\n"; } else { $guess .= " Number of Wrong Guesses Left: <B>".($max-$wrong)."</B><BR>\n<H1><font size=5>\n$word_line</font></H1>\n<P><BR>Choose a letter:<BR><BR>\n$links\n"; } } else { $n = rand (1,count($words)-1); if ($n>(count($words)-1)) $n=0; $win .= "<BR><BR><H1><font size=5>\n$word_line</font></H1>\n<P><BR><BR><B>Congratulations $bbuserinfo[username]!!! You have won!!!</B><BR><BR><BR>\n<A HREF=$PHP_SELF?n=$n>Play again</A>\n\n"; } $navbits = construct_navbits(array('' => $vbphrase['vb_hangman'])); $navbar = render_navbar_template($navbits); // ###### YOUR CUSTOM CODE GOES HERE ##### $pagetitle = 'hangman'; // ###### NOW YOUR TEMPLATE IS BEING RENDERED ###### $templater = vB_Template::create('hangman'); $templater->register_page_templates(); $templater->register('hangmanwords', $hangmanwords); $templater->register('hangmanmax', $hangmanmax); $templater->register('hangmanguests', $hangmanguests); $templater->register('letters', $letters); $templater->register('n', $n); $templater->register('additional_letters', $additional_letters); $templater->register('words', $words); $templater->register('alpha', $alpha); $templater->register('all_letters', $all_letters); $templater->register('wrong', $wrong); $templater->register('word', $word); $templater->register('done', $done); $templater->register('word_line', $word_line); $templater->register('c', $c); $templater->register('links', $links); $templater->register('max', $max); $templater->register('x', $x); $templater->register('sorry', $sorry); $templater->register('play', $play); $templater->register('win', $win); $templater->register('term', $term); $templater->register('guess', $guess); $templater->register('nwrong', $nwrong); $templater->register('navbar', $navbar); $templater->register('pagetitle', $pagetitle); print_output($templater->render()); ?> |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|