Smarty Gravatar plugin

I realize the need for this when working on an MVC based CMS especially with Smarty engine in it.

Oh and in case you’re wondering why its sounds like the upcoming movie Avatar by James Cameron, I don’t know. What it has to do with internet term ‘avatar‘? I really don’t know. But in ‘www’, Gravatar is a short term for “Globally Recognized Avatar” assigned by a user as his/her default avatar in every places of the internet, same people behind WordPress. There are other similar services too.

So, this plugin is based from its documentation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * Gravatar
 *
 * @link http://www.marvinmarcelo.com
 *
 * @param array $params
 * @param Smarty $smarty
 */
function smarty_function_gravatar($params, &$smarty)
{
  /**
   * constant $gravatar_host
   */
  $gravatar_host = "http://www.gravatar.com/avatar/";
 
  if ( !isset($params['email']) )
  {
    $smarty->trigger_error("gravatar: email parameter not set");
    return;
  }
 
  if ( !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $params['email']) )
  {
    $smarty->trigger_error("gravatar: {$params['email']} is not a valid email.");
    return;
  }
  /**
   * @link http://en.gravatar.com/site/implement/url
   */
  $hash = strtolower(md5(trim($params["email"])));
  $src = $gravatar_host . $hash . ".jpg?";
 
  if ( !isset($params["size"]) || !($params["size"] >= 1 && $params["size"] < =512 ))
  {
    $params["size"] = 32;
  }
  $src .= "s={$params["size"]}";
 
  if ( !isset($params["default"]) )
  {
    $params["default"] = "identicon";
  }
  $src .= "d={$params["default"]}";
 
  if ( !isset($params["rating"]) )
  {
    $params["rating"] = "G";
  }
  $src .= "r={$params["rating"]}";
 
  $extras = "";
 
  if ( isset($params["class"]) )
  {
    $extras .= " class=\"{$params["class"]}\"";
  }
 
  if ( isset($params["id"]) )
  {
    $extras .= " id=\"{$params["id"]}\"";
  }
  /**
   * @example http://www.gravatar.com/avatar/7023218434f12aee57f4b03454dadcaa?s=32&d=identicon&r=G
   */
  print "<img src=\"$src\" alt=\"{$params["alt"]}\"$extras />";
}

Simply extract function.gravatar.zip into your Smarty plugin directory. And use it in your template file like this

{gravatar email='someemail@domain.com'}

You’re done!

Fix unresponsive browser when serving files via PHP

After an hour long reading his frustrations over IM, I began google’ing on files served thru PHP but locks clicks in a client’s browser until download is finished. It is unresponsive until the download is complete.

// add this function before a readfile() or chunked with fopen()
session_write_close();
// header goes here, content-type, filename, etc
readfile($filepath);

And, also, I found this class to support downloading using a download manager.

Smarty template for generating cascaded UL based menu

So far, I’ve been happy how this template behaves when I pass a parent/child links into it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< {$parent_tag|default:'ul'}{if $menu_id} id="{$menu_id}"{/if}{if $parent_class} class="{$parent_class}"{/if}>{* ul tag opens here *}
{foreach from=$links item=link}
  {if $link.class_active && $link.href == $current_uri}
    {if $link.class}
      {assign var=class value="`$link.class` `$link.class_active`"}      
    {else}
      {assign var=class value="`$link.class_active`"}
    {/if}
  {else}
    {assign var=class value="`$link.class`"}
  {/if}
  < {$child_tag|default:'li'}{if $class} class="{$class}"{/if}{if $link.id} id="link_{$link.id}"{/if}>
    <a href="{$link.href}"{if $link.target} target="{$link.target}"{/if}>{$link.text}</a>
  {if $link.children}
    {include file="links.html" links=$link.children parent_class=""}
  {/if}
{/foreach}
< /{$parent_tag|default:'ul'}>

Does something like this when rendered

<ul class="sf-menu">
  <li><a href="http://www.swerteka.com/index.php">Home</a></li>
  <li><a href="http://www.swerteka.com/PostItem">Post Item</a></li>
  <li><a href="http://www.swerteka.com">Blog</a>
    <ul>
      <li><a href="http://www.swerteka.com/BlogComments/do/viewrecent">Comments</a></li>
    </ul>
  </li>
  <li><a href="http://www.swerteka.com/Content/name/about">About</a>
    <ul>
      <li><a href="http://www.swerteka.com/Content/name/contact">Contact Us</a></li>
    </ul>
  </li>
</ul>

OOP 101: No logic in template, as much as possible

If you have a repeating region in your template and it doesn’t need a wrapper around it, simply use section

1
2
3
{section name=s loop=$images}
   <div id="image_{$images[s].id}"><img src="{$images[s].src}" alt="" /></div>
{/section}

Cases where you need html wrap tags is like this

1
2
3
4
5
6
7
8
9
{if $navlinks}
<ul>
{section name=s loop=$navlinks}
   <li id="navitem_{$navlinks[s].id}">
     <a href="{$navlinks[s].href}">{$navlinks[s].text}</a>
   </li>
{/section}
</ul>
{/if}

This avoids the <ul> being rendered if $navlinks don’t have a return record. Another way is to access Smarty properties

1
2
3
4
5
6
7
{section name=s loop=$navlinks}
   {if $smarty.section.s.first}<ul>{/if}
   <li id="navitem_{$navlinks[s].id}">
     <a href="{$navlinks[s].href}">{$navlinks[s].text}</a>
   </li>
   {if $smarty.section.s.last}</ul>{/if}
{/section}

But still, this adds an if in the template. As this post title goes, you should avoid them, that’s the basics of OOP system and MVC frameworks, separate logic from code :D

What made me write this post? Template Designers sometimes hate seeing if statements. In my case, my boss transforms into savvy Designer as well, at times! Bummer

My first project for open-source release

Welcome to SourceForge.net!

The following project has been approved for hosting:

Descriptive Name: ******
Unix Name: ******

By using the SourceForge.net site, you agree to be bound by the
terms and conditions of the SourceForge.net Terms of Use Agreement.

SourceForge.net project hosting is available solely to Open Source
software development projects. If you are not releasing your project
under an Open Source license, please contact SourceForge.net staff for
assistance (see “Support”, below).

Project Administration:

To manage your project, login and go to:

https://sourceforge.net/project/admin/?group_id=******

For help in getting started with your new project:

http://p.sf.net/sourceforge/getstarted

Support:

SourceForge.net maintains a full-time support team; we are ready to
assist you with any questions you have about your new project.

We can be contacted by submitting a ticket at:

http://p.sf.net/sourceforge/submitticket

We may also be reached directly during business hours via IRC:

http://p.sf.net/sourceforge/irc

– the SourceForge.net crew

Kicking the bad habits of Project Management

Excerpts from devshop creator Craig Fitzpatrick

Habit   Identified as
Estimating how long work takes => Time estimation error
Pure dev time versus fighting fires => Distraction rate
Premature speculation (we make a bunch of promises too early in the process when we don’t have enough information) => Schedule confidence
Requirements and schedule falling out of sync. => Tie requirements and designs to tasks / estimates
Not planning for change. => Turn / Context switching

Source : http://blip.tv/file/1200648

A safe thought

“You are not the only one who can read, write, understand and extend codes in this world”

Well this line does not only help you to understand how you get replaced by someone else better than you. But it also helps you leave your post and get rid of your boss! :D