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
Hello, I'm actually having
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:
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
Well I found one error: <?php
Well I found one error:
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
Well, as for what you're
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...
First, you should pass a
First, you should pass a string to variable_set() and variable_get(). You need quotes around the variable name:
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:
Then just populate your template variable in the template code:
Finally, make sure you access your variable by the correct name and as an array in the .tpl file, not as an object:
Ack. We've just replied to an
Ack. We've just replied to an 18 month old thread...
Hey guys! Thanks for the
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.