Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...

When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store
Related Articles

Other Uses For Smarty

More Uses For Smarty Templates

Web pages and email templates aren’t the only place to use Smarty templates. Here are a few more ideas for where they can be used. I haven’t included code here as the general algorithm will be similar to the email example on the previous page.

Creating XML and/or RSS Feeds

You could create a template to output XML. I used this on a web site where I had made a custom XML schema. After all, HTML is really just a form of XML anyway.

In the email example, we sent the output through the mail() function. In this case, you could just use Smarty’s display() method if you’re just outputting the XML directly. Just remember, you’ll need to send a content-type header first.

Listing 4 listing-4.php
    // ... other code
    header('Content-type: text/xml');

Building Data Storage Files Such As CSV

Comma Separated Values files are commonly used to easily transport data between applications. For example, let’s say you want to import a list of your web site users into Excel, or to send them to an advertiser. You could use a template such as this:

Listing 5 listing-5.tpl
User ID,First Name,Last Name,Email
{foreach from=$users item=row}

This is fairly straightforward to put straight into your code, but if your web site already uses Smarty, why not put it to use. Using a template like this, the format is really easy to control without having to hack at your PHP files.

Auto-Generated SQL Schemas

In a web application I wrote several years, the site was designed so for each client that was added to the database, a separate set of tables was created (this may or may not have been the best design, but that’s beside the point). To uniquely distinguish each set of tables, the client’s ID was stored in the table name.

To create this custom set of tables, the schema was stored in a Smarty template. Then when a client was added, the template would be run through Smarty, and the generated schema was executed by PostgreSQL.

Listing 6 listing-6.tpl
create table user_{$user_id}_sometable (
    /* sql column definitions */

PHP Code Templates

Code generators are a good tool to create source code files or classes that need to be created fairly often. A previous article that I wrote for PhpRiot was about managing your data with DatabaseObject. It wasn’t included with the article, but I have previously written a code generating tool to create child DatabaseObject classes.

The actual code is stored in a Smarty template, and the tool simply outputs PHP code which I wrote to PHP file.

A snippet of this template looks like this:

Listing 7 listing-7.tpl
    class {$class} extends DatabaseObject
        function {$class}(&$site)
            parent::DatabaseObject($site, '{$table}', '{$primaryKey}');

This code will make more sense if you have read the DatabaseObject article. The rdelim and ldelim tags are used because { and } are reserved in Smarty.

In This Article