“In three words I can sum up everything I've learned about life: It goes on.”
– Robert Frost

Posts Tagged ‘smarty’


25
Sep

I had to come up with this post because I’ve been working on an old CMS about 3 years old (or even more) and I had been wondering why the calendar links cuts off from 2001 to 2003.

Task: List a year/month links from the present to the oldest published image sets.

Sounds easy, and I believe it is.

But when I checked on the existing script, this is what I got.

 $month_array = array("1"=>'January',"2"=>'February',"3"=>'March',"4"=>'April',"5"=>'May',"6"=>'June',"7"=>'July',"8"=>'August',"9"=>'September',"10"=>'October',"11"=>'November',"12"=>'December');
	$smarty->assign('y2day',$y2day); $y=0;
	for ($q=12; $q >= 1; $q--){
			$month = $month_array[$q];
			if( ($_GET[year] < '2000') && ($m2day ==$q ) ) $w_string = $week_string; else  $w_string ='';
			if( ($_GET[year] == '2007') && ($_GET[month] == $q) ) $w_string = $week_string;
 
		  if($m2day >= $q){
			$tmp_cal_month_now_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_now_array[$y++] =  $tmp_cal_month_now_array;	 }
		} //for
 
	 $smarty->assign('calendar_now_month',$date_month_now_array);
 
	$y=0;	$c_yeartoday = date("Y") -1; //echo $c_yeartoday;
	for($q=0; $q < = 5; $q++){
			$tmp =$c_yeartoday - $q; //echo $tmp;
 
		 $tmp_cal_date_array = array(
					  'year' => $tmp
 
				 );  $date_year_array[$y++] =  $tmp_cal_date_array;
	 } //for
 
	$smarty->assign('calendar_year',$date_year_array);
 
	    $y=0;
		for($q=12; $q >= 1; $q--)
		{
			$month = $month_array[$q];
			if( $_GET[month] == $q ) $w_string = $week_string; else $w_string='';
			$tmp_cal_month_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_array[$y++] =  $tmp_cal_month_array;
 
		}//for
	 $smarty->assign('calendar_month',$date_month_array);
 
	 $y=0;
		for($q=12; $q >= 10; $q--)
		{
			$month = $month_array[$q];
			if( ($_GET[year] == '2000') && ($_GET[month] == $q) ) $w_string = $week_string; else $w_string='';
			$tmp_cal_month_old_array = array(
					'month' => $month,
					'string' => $w_string,
					'link' => $q
					); $date_month_old_array[$y++] =  $tmp_cal_month_old_array;
 
		}//for
	 $smarty->assign('calendar_month_old',$date_month_old_array);

(more…)

26
Aug

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!

04
Jul

I just got the title from the source, read it yourself :)

This options worked for me! I’ve been having trouble trying tidy on my CMS. But it only renders tidy’ed the content inside <body> and worst, everything outside it are stripped off – no title, and meta’s neither opening and closing html

$config = array(
  'indent'      => true,
  'output-xml'  => true,
  'input-xml'   => true,
  'wrap'        => '1000'
);

For detailed options and their uses, look at their quickref

10
Jun

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>

20
Apr

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