Random Links Using xenForo Template Sytax

trichome

Member
In the footer of my forum I'd like to display a list with 3 links randomly pulled from a pool.

Here's the PHP code I've been using:
PHP:
<?php
function listLinks()
{
$linkpool=array(
'<a href="#">link 01</a>';
'<a href="#">link 02</a>';
'<a href="#">link 03</a>';
'<a href="#">link 04</a>';
'<a href="#">link 05</a>';
'<a href="#">link 06</a>';
);
 
$rand_keys = array_rand($linkpool, 3);
echo "\t\t<li class=\"a\">" . $linkpool[$rand_keys[0]] . "</li>\n";
echo "\t\t<li class=\"b\">" . $linkpool[$rand_keys[1]] . "</li>\n";
echo "\t\t<li class=\"c\">" . $linkpool[$rand_keys[2]] . "</li>\n";
}
 
listLinks();
?>

Basically 3 list items, each with a unique css class with a random, unique link in each.
Is there a way to achieve the same result using the following code which was posted here?

Code:
<xen:comment>DEFINE AND RANDOMIZE YOUR BANNERS</xen:comment>
 
<xen:set var="$banners.1">banner one</xen:set>
<xen:set var="$banners.2">banner two</xen:set>
<xen:set var="$banners.3">banner three</xen:set>
<xen:set var="$banners.4">banner four</xen:set>
<xen:set var="$banners.5">banner five</xen:set>
 
{xen:raw '$banners.{xen:calc '({$serverTime} % 5) + 1'}'}
 
I just attempted this but it has proven difficult. There is no rand function in the templates so I use the serverTime, but that only yields one random number. So I tried to use that one number to specify the start of a range inside of the banners array, but I had trouble maintaining a counter due to some weird scoping behavior.

This is much easier with PHP code.
 
Thanks for the reply. I think the answer is to create an addon and use template hooks and a callback so I can just include that PHP snippet itself. Still trying to figure that process out.
 
Thanks Jake, that post helped clarify the process.

It seems to be working except I can't figure out how to get the list to display in the correct position; it's always inserted at the very beginning of the page and not after the hookName. I'm sure I'm putting my function in incorrectly but being PHP illiterate I can't figure it out.

PHP:
<?php
function listLinks()
{
$linkpool=array(
'<a href="#">link 01</a>';
'<a href="#">link 02</a>';
'<a href="#">link 03</a>';
'<a href="#">link 04</a>';
'<a href="#">link 05</a>';
'<a href="#">link 06</a>';
);
 
$rand_keys = array_rand($linkpool, 3);
echo "\t\t<li class=\"a\">" . $linkpool[$rand_keys[0]] . "</li>\n";
echo "\t\t<li class=\"b\">" . $linkpool[$rand_keys[1]] . "</li>\n";
echo "\t\t<li class=\"c\">" . $linkpool[$rand_keys[2]] . "</li>\n";
}
 
class Demo_Listener
{
  public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_A$
  {
      if ($hookName == 'footer')
      {
        $contents .= listLinks();
      }
  }
}
 
I know it's annoying to have people just ask for the solution but I've been trial and erroring at this for hours with no success. Could you give me an example of how this would be done?
 
Rich (BB code):
function listLinks()
{
$linkpool=array(
'<a href="#">link 01</a>';
'<a href="#">link 02</a>';
'<a href="#">link 03</a>';
'<a href="#">link 04</a>';
'<a href="#">link 05</a>';
'<a href="#">link 06</a>';
);
 
$rand_keys = array_rand($linkpool, 3);
return "\t\t<li class=\"a\">" . $linkpool[$rand_keys[0]] . "</li>\n"
. "\t\t<li class=\"b\">" . $linkpool[$rand_keys[1]] . "</li>\n"
. "\t\t<li class=\"c\">" . $linkpool[$rand_keys[2]] . "</li>\n";
}
 
Ah, concatination! It was working with one return there but I couldn't figure out how to get all the list items in it. Thank you!
 
Top Bottom