I'm developing my first website with Drupal. So far it's going well but I have a few details I haven't figured out.
I want to load a random variable on each page load, in order to load a different color scheme each time.
My template is called 'fest' so I put this in my template.php:
$color = $colors[array_rand(Array('red', 'blue', 'magenta', 'green' ), 1)];
$variables['color'] = $color;
If I put it in fest_preprocess_html
the color var can be accessed in html.tpl.php but not in page.tpl.php. I want that variable available in node, page, html, everywhere.
Putting this code in plain function fest_preprocess
didn't work either.
What am I doing wrong?
--EDIT Check @Berdir's answer, which led me to resolve this issue. This is the code I used after all:
function mytemplate_get_color() {
static $color = NULL;
if (!$color) {
$colors = Array('red', 'blue', 'magenta', 'green');
$rand_key = array_rand($colors, 1);
$color = $colors[$rand_key];
}
return $color;
}