Posts tagged as:

open-source

Vacation Learning – PHP and Smarty Templates

by Ron Bieber on Thursday, December 20, 2007

I’m on vacation this week and next week. Since I rarely have time to learn anything technical (or blog for that matter anymore), I thought I would take some time during my time off to learn something new around development.

We have a system at work that is essentially a small portal. The core of it was written by me to learn PHP about 8 years ago and has been augmented by me and one other guy at workKeith and I over the years. Over that time, as we added new functionality to it, I used it to experiment with other languages as I was learning it. Other pieces were written in Java out of convenience. In total, we have pieces written in PHP, Java, Python, and PERL.

As I usually use this system to learn new things, I figured it would be a good candidate to use to learn how to use the Smarty templating system for PHP. I became interested in this templating system after working with Eventum over the last few weeks and figured that if I am going to do further work with Eventum, it would be helpful to understand the templating framework it uses.

So I’ve started using the system to take our 8 year old PHP code base and separate some of the presentation logic out. Smarty is pretty flexible and easy to use at a high level (I haven’t gotten into any of the really advanced stuff yet).

Here’s an example of how nicely the use of a templating system simplifies your code. Take this example, which enumerated entries from our internal wiki via an RSS feed into a section on the home page:


function getWikiEntries($url) {
$theHTML = "";

$rss = fetch_rss($url);

$theHTML .= "

";
$theHTML .= "

";

# foreach over each item in the array.
# displaying simple links

$rowCount = 0;
$className = "modifications-evenrow";

foreach ($rss->items as $item ) {

if (($rowCount % 2) == 0) {
$theHTML .= "

";
}
$theHTML .= "

";
} else {
$theHTML .= "

";
}
$rowCount++;

if ($rowCount == 20):
break;
endif;
}

$theHTML .= "

";

# get the channel title and link properties off of the rss object
#
$title = "Recent Wiki Entries";
$link = $rss->channel['link'];

#$theHTML .= "$title";
$theHTML .= "$title   ";
$theHTML .= "

 
";
$theHTML .= "";

# truncate item title to 28 characters
$myTitle = $item['title'];

if (strlen($myTitle) > 28 ) {
$myTitle = substr($myTitle, 0, 28) . " ...";
}

$theHTML .= $myTitle;
if (($rowCount % 2) == 0) {
$theHTML .= "
  

";

return($theHTML);
}

I’m sure you can appreciate how hard this would be to maintain, and all of the cruft that has accumulated over the years …

Now take the simplified version (sans error checking), written today in about 10 minutes:

function getWikiEntries($url) {
$rss = fetch_rss($url);

$template = new TemplateEngine();

$firstColumn = array_slice($rss->items, 0, 10);
$secondColumn= array_slice($rss->items, 10);

$template->assign("firstColumn", $firstColumn);
$template->assign("secondColumn", $secondColumn);
$template->assign("link", $rss->channel['link']);

return($template->renderString("wikiEntries.tpl"));
}

… along with its corresponding Smarty template:

{section name="entries" loop="$firstColumn"}

{/section}

Recent Wiki Entries  
 
{$firstColumn[entries].title|truncate:28:" ..."}   {$secondColumn[entries].title}

I don’t know about you, but I think thats quite a difference in maintainability. I’d much rather modify the html in the template than in the original function. Not only that, but the code is actually code, not a bunch of code with a lot of simply horrid markup stuck in the middle of everything.

I’m pretty impressed with how much I’ve been able to use in a short amount of time this week. The libraries are obviously thought out and ramp up time for me was really minimal. I like libraries like that. It also addresses something that has annoyed me for a long time. Embedded HTML is a pain to maintain and I’ve dreaded going into this over the years just because of that.

At some point, I’ll investigate what it takes to write custom plugins, a functionality that the libraries also support.

I think I’ve been able to get a really good start at getting something maintainable. My goal over the next few of weeks is to templatize the whole system, then start taking the non-PHP pieces of the system and rewrite them in PHP. I’ll also add the ability to change configuration in one place, so that we can cut some of the pain that we have in keeping things maintained down – and perhaps be able to install the application in other places.

Should be fun. I’m definitely feeling productive over the past few days. I’ve always liked working in PHP over other languages. I definitely have to do work like this more often.

[tags]php,development,software,open-source[/tags]

{ 2 comments }

LDAP Enabling The Eventum Defect Tracking System

October 20, 2007

Due to a recent reorg, I have the opportunity to replace our defect tracking system, which has quite a bit of really wasteful process baked into the tool, with a new one. I’ve been looking at defect tracking software for a while, and chose Eventum, an open source project by MySQL AB [...]

Read the full article →

Video: How Open Source Projects Survive Poisonous People (And You Can Too)

April 5, 2007

Since getting a 80G iPod about a month ago two weeks ago, I’ve been really getting into watching the Google Tech Talks on Google Video. I recently watched How Open Source Projects Survive Poisonous People (And You Can Too), a lecture given by Brian Fitzpatrick and Ben Collins-Sussman from the Subversion team [...]

Read the full article →

Free Web Site Monitoring at mon.itor.us

January 6, 2007

I’ve created an account at mon.itor.us, a free web site monitoring tool I found in Cote’s Flickr feed. Looks interesting.

Read the full article →

Open Source Alarm Clock?

January 1, 2007

Jonna pointed me to Chumby, an open source alarm clock. According to the article, the clock will run for $150 and because of its open source nature, will have an “array of downloadable, hackable widgets”.

Read the full article →

Sun Hires JRuby Development Team

September 9, 2006

Tim Bray talks about the recent hiring of the JRuby development team by Sun Microsystems. I think this is awesome. I’m so impressed with the Ruby language and would love to see it operate in a container and leverage all of the great Java components out there. Cote also [...]

Read the full article →

SQLYog MySQL Client Open Sourced

September 5, 2006

Got an email this morning stating that SQLYog, an excellent MySQL client comparable to TOAD for Oracle, is now open sourced. Go grab it!

Read the full article →

FreeDOS 1.0 Released

September 5, 2006

If you are looking for a free replacement for MS-DOS, look no further. FreeDOS 1.0 has been released.

Read the full article →

Vienna – An Open Source NewsReader For The Macintosh

August 27, 2006

I had mentioned in a post earlier this year that I have outsourced many of the tools that I use to third party vendors. Google Reader was one of the applications that I started using.
Unfortunately, I’m not a big fan of the “river of news” type of newsreaders, and would rather see a [...]

Read the full article →

Agassi: MySQL will support SAP this year

April 11, 2006

In this article, Shai Agassi predicts that the open source database MySQL will be certified to run SAP by the end of the year.

Read the full article →

Red Hat scoops up JBoss

April 11, 2006

Red Hat scoops up JBoss | CNET News.com

Read the full article →

What Corporate Projects Should Learn from Open Source

March 2, 2006

OnLamp had an excellent article yesterday called What Corporate Projects Should Learn from Open Source. The articles pretty long, but well worth the read. While there are obvious differences in the two types of projects (like budgets and deadlines), I still believe that corporations can move closer to the OSS model [...]

Read the full article →

Yahoo open sources UI Library and Design Patterns

February 14, 2006

Yahoo has released their User Interface Libraries and Design Pattern Libraries to the development community. The User Interface Libraries are released under a BSD license, while the Design Pattern Libraries are released under Creative Commons Attribution License.

Read the full article →

Transparent Commodity Infrastructure and Web 2.0

February 13, 2006

Tom the Architect pointed me over to this article called Transparent Commodity Infrastructure and Web 2.0. Excellent piece.
I especially like this quote here:

Let me use an example: back in 1998 if you were building a web-based startup, you were probably running on Solaris/SPARC and using an Oracle database. You were also likely to [...]

Read the full article →

Oracle’s Open-Source Shopping Spree

February 10, 2006

Oracle’s Open-Source Shopping Spree – Is Oracle looking to buy JBoss, Zend, and Sleepycat Software?

Read the full article →

Thunderbird is go | The Register

January 16, 2006

This article over on The Register talks about the latest version of Mozilla Thunderbird and its support of RSS and, yes folks, podcasting. For more information, check out the release notes for the 1.5 release.

Read the full article →

MySQL Gets Government Thumbs Up

January 13, 2006

MySQL Gets Government Thumbs Up – General Services Administration signs five-year contract with MySQL AB.

Read the full article →

JavaSVN 1.0.1 Release

December 15, 2005

I posted about the initial release of this software a while back, but I’m really getting the urge to see what it would take to use JavaSVN, a 100% Pure Java implementation of Subversion as the beginnings of a content management solution. Version 1.0.1 was announced yesterday afternoon.

Read the full article →

How Microsoft Can Compete Against Open Source

November 13, 2005

Scott Johnson, a cofounder of Feedster.com writes an open letter to Robert Scoble articulating his ideas on How Microsoft Can Compete Against Open Source. Interesting read and makes a lot of sense.

Read the full article →

Open Source, Open Wallet

November 7, 2005

This article talks about the rise in VC interest in companies whose products are based on an open source model.

Read the full article →