Incodius™ News


What's happening in our world of creative web development.



e:info@incodius.co.nz

..........................................................................................
WE TWEET:  incodius Google fonts have added lots of new fonts! http://www.google.com/webfonts


« Back

How smart developers make the most of Smarty



At Incodius, many of our custom PHP applications use Smarty templates to cleanly separate our application code (the thinking part of the website, that finds the latest news articles, performs searches or fills your shopping cart) from the presentation code (everything that appears on your screen). When you're viewing this article, what you're seeing is content that was stored in our MySQL database, retreived by code written in PHP, and displayed for your viewing pleasure using HTML, Javascript and CSS. In our applications, this HTML part is stored and maintained separately from the rest in Smarty templates.

 

 

The benefit of using templates is that we can develop the front end of the site, including all the fancy animations and complex layout effects, independently of the complex back-end application code. Once we have this layout consisting of images, HTML, CSS and Javascript, we test it thoroughly to ensure it's running smoothly in all browsers (yes, even IE6!). Then we integrate it with our PHP by replacing our 'Lorem Ipsum' test content and anything else that will be dynamically generated with Smarty tags, that look like this: {$content}. With the exception of these tags, the final templates are coded just like pure HTML, which makes them easy for other less-codey people (designers with little coding experience who are working in Dreamweaver, for example) to make changes to them.

 

For designers crossing over to front-end web code, this system is a great way to quickly convert existing static HTML sites into dynamic PHP applications. It's incredibly quick to put an existing layout into a Smarty template, and it can cut down development time dramatically to create the main page in HTML and then hand it to us where we can use it as the template for a simple PHP application running Smarty, and the other pages on the site can be done through the CMS rather than tedious copying and pasting for separate HTML files for each page. It also means that you don't need to know anything about PHP to offer dynamic sites with CMS to your clients: once the site is up you can change the template,  FTP a new version up, and the changes will apply to all pages that use that template (you can have multiple templates in the site as needed, and templates within templates!). And, the templates are in code that you're used to, with no curly bits!

 

 

If you're a PHP developer thinking about using Smarty, we highly recommend it. Smarty comes with a range of built-in functions, but it's also very easy to write your own. Here's one we wrote recently that puts the latest tweet from a twitter account associated with a website into the template simply by writing {latestTweet} into the HTML. These plugins go in the 'plugins' folder in the Smarty's libs directory. If you're a developer who is new to Smarty you'll want to read up on it here.  To get you started on plugins, here's the code for our basic 'Latest Tweet' plugin. You can see a more advanced version of this in the top right of the Incodius site now - we've added some link detection to make the links clickable.

 

/*
 * Smarty plugin
 * ————————————————————-
 * File:         function.latestTweet.php
 * Type:       function
 * Name:     latestTweet
 * Purpose:  returns the latest tweet from the site's twitter
 * ————————————————————-
 */
 
 
 function smarty_function_latestTweet($params, &$smarty) {
    // Your twitter username.
    $username = "yourname";
     
    $feed = "http://twitter.com/statuses/user_timeline/".$username.".xml?count=11";
     
    function parse_feed($feed) {
        $xml = new SimpleXMLElement($feed);
        $status = $xml -> status;
        $tweet =  $status -> text;
        $tweet = str_replace("<", "<", $tweet);
        $tweet = str_replace(">", ">", $tweet);
        return $tweet;
    }
     
    $twitterFeed = file_get_contents($feed);
    return parse_feed($twitterFeed);   
   
}
?>

 

That's all there is to it!

 


« Back