Experience is the best teacher!

merlinofchaos's picture

I was just looking through add1sun's latest commits, and I noticed a few things that could stand a little improvement.

I took this function:

<?php
function phptemplate_user_picture($account) {
if (
variable_get('user_pictures', 0)) {
    if (
$account->picture && file_exists($account->picture)) {
     
$picture = file_create_url($account->picture);
    }
    else if (
variable_get('user_picture_default', '')) {
     
$picture = variable_get('user_picture_default', '');
    }
    if (isset(
$picture)) {
     
$alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
     
$picture = theme('image', array('path' => $picture, 'alt' => $alt, 'title' => $alt, 'attributes' => '', 'getsize' => FALSE, ));
      if (!empty(
$account->uid) && user_access('access user profiles')) {
       
$picture = l($picture, "user/$account->uid", array('title' => t('View user profile.')), NULL, NULL, FALSE, TRUE);
      }
      return
_phptemplate_callback('user/user_picture');
    }
  }
}
?>

And made some modifications. /** indicates an annotation just for here.

<?php
function phptemplate_user_picture($account) {
 
// fill default variables.
  //** Try to fill in default $vars right away. Put in anything that seems relevant. Always try to
  //* put in all arguments to the function at least, so that the template author can have all
  //* the data to work with.
 
$vars = array(
   
'target' => "user/$account->uid",
   
'title' => t('View user profile.'),
   
'account' => $account,
  );
  if (
variable_get('user_pictures', 0)) {
    if (
$account->picture && file_exists($account->picture)) {
     
$picture = file_create_url($account->picture);
    }
    else if (
variable_get('user_picture_default', '')) {
     
$picture = variable_get('user_picture_default', '');
    }
    if (isset(
$picture)) {
     
$alt = t("@user's picture", array('@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous'))));
     
$vars['picture'] = theme('image', array('path' => $picture, 'alt' => $alt, 'title' => $alt, 'attributes' => '', 'getsize' => FALSE, ));
     
//** Let's allow the template author to use either the link or not; it costs us very
      //* little to make them both available.

     
if (!empty($account->uid) && user_access('access user profiles')) {
       
$vars['picture_link'] = l($vars['picture'], $vars['target'], array('title' => $vars['title']), NULL, NULL, FALSE, TRUE);
      }
      else {
       
$vars['picture_link'] = $vars['picture'];
      }
      return
_phptemplate_callback('user/user_picture', $vars);
    }
  }
}
?>

Aside from the annotations, it's also important to remember while writing this stuff that unless you put it in $vars it's probably not going to make it to the actual template, so please watch your variable names.

Login to post comments

Thanks

add1sun's picture
add1sun - Tue, 2007-02-13 00:30

Thanks merlinofcahos, I actually kind of bailed in the midst of the user changes I was getting lost in and just committed some stuff so I wouldn't lose the mess I had. The more examples I see the more clearly I can understand where I'm trying to get to. - n00b tries hard - think, think, think...