I'm trying to use template.php to pass some variables to my page-front.tpl.php but am having some problems. Here is the story as told by the code:

In a custom module, I set a variable called $featured_fam that stores a uid for later data retrieval:

<?php
function featuredfamily_cron() {
	$newfamily = db_result(db_query("SELECT uid FROM {advprofile} WHERE status = 'Active' ORDER BY RAND() LIMIT 1"));
	variable_set(featured_fam, $newfamily);
} ?>

Don't worry, advprofile is a table I've set up elsewhere that is fully functioning, and not the source of this problem (I'm 99.9% sure on that).

Ok, moving along... In my template.php file, I've put the following code to retrieve all the data I'll need:

<?php
function iheartadoptionv1_preprocess_page(&$vars, $hook) {
	$featured_uid = variable_get($featured_fam, 1);
	
	$vars['ffdata'] = array(
		'name' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 1", $featured_uid)),
		'city' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 2", $featured_uid)),
		'state' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 3", $featured_uid)),
		'phone' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 4", $featured_uid)),
		'intro' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 5", $featured_uid)),
		'picture' => db_result(db_query("SELECT picture FROM {users} where uid = %s", $featured_uid)),
		);
						  
} ?>

Now here is the first sign of trouble. When I go into phpmyadmin to check out the variables table, I see featured_fam sitting there looking good with a valid uid... but I don't see anything set for ffdata. Assuming I know not what I'm doing when I set $vars, I proceed to my page-front.tpl.php file with this code:

Simplified for you, my friends
<!-- FEATURED FAMILY SECTION -->
        <div class="ff-content">
        	<?php 	print($ffvars->name);
					print($ffvars->city);
					print($ffvars->state);
					print($ffvars->phone);
					
			?>

But when I load up the page, nothing prints, implying that $ffvars is an empty array.

Anyone notice what I'm doing wrong here?

Many thanks!
- Ryan

Comments

jurc’s picture

Hello,
I'm actually having the same problem as you are; I'm trying to pass a variable from theme_preprocess_block to block.tpl.php. To put it simple:

<?php
function theme_preprocess_block(&$vars) 
{
   $vars['my_variable'] = "my_value";
}
?>

Testing the $my_variable in block.tpl.php gives an "Undefined variable" error. So what am I (are we) doing wrong?
I'm using drupal 7, latest nightly build.

Best regards,
Jurc

rschwab’s picture

Well I found one error:

<?php $featured_uid = variable_get($featured_fam, 1);

should be

$featured_uid = variable_get(featured_fam, 1);
?>

Also, silly me... notice in the template.php my array is called $ffdata, then on the tpl.php its called $ffvars. That would be problematic.

- Ryan

1kenthomas’s picture

Well, as for what you're doing wrong, you're hardcoding db calls into a theme template, which is just very wrong for 10 reasons.... starting with abstraction... data/theme separation... security...

johnpitcairn’s picture

First, you should pass a string to variable_set() and variable_get(). You need quotes around the variable name:

<?php
  variable_set('variable_name', $value); // note the quote marks
?>

But if you don't need the data to be persistent between page loads (do you?), you probably shouldn't be using variable_set() to cache your data, since this will store them in the database variables table. And database calls shouldn't be in template code anyway.

One way to approach this sort of thing is to make a public utility function in your module code that includes a static array. If the array is empty, make your database calls, store the values and return the array. If the array isn't empty, just return the array. So:

<?php
/*
 * Implementation of hook_init().
 */
function featuredfamily_init() {
  // make your database calls at page-load init
  featuredfamily_get_family();
}

/*
 * Loads and caches family data.
 */
function featuredfamily_get_family() {
  static $family; // static variables are persistent from one function call to the next

  if (!$family) {
    $featured_uid = db_result(db_query("SELECT uid FROM {advprofile} WHERE status = 'Active' ORDER BY RAND() LIMIT 1"));
    $family = array(
        'name' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 1", $featured_uid)),
        'city' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 2", $featured_uid)),
        'state' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 3", $featured_uid)),
        'phone' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 4", $featured_uid)),
        'intro' => db_result(db_query("SELECT value FROM {profile_values} WHERE uid = %s AND fid = 5", $featured_uid)),
        'picture' => db_result(db_query("SELECT picture FROM {users} where uid = %s", $featured_uid)),
    );
  }
  
  return $family;
}
?>

Then just populate your template variable in the template code:

<?php
function iheartadoptionv1_preprocess_page(&$vars, $hook) {
  // check in case your module is disabled or missing
  if (function_exists('featuredfamily_get_family')) {
    $vars['ffdata'] = featuredfamily_get_family();
  }
}
?>

Finally, make sure you access your variable by the correct name and as an array in the .tpl file, not as an object:

<?php
  print $ffdata['name']; // note print doesn't need brackets, it's not a function
?>
johnpitcairn’s picture

Ack. We've just replied to an 18 month old thread...

rschwab’s picture

Hey guys! Thanks for the followup. But you're right, this thread is old and the problem in it solved long ago. Your point about separating database calls out of theme code is a good one.