Let me say this first: I do not believe in big bloated MVC PHP frameworks. I believe in having custom code that runs as fast as possible. For recurring useful functionality, such as templates, I believe in using simple, efficient and flexible dedicated standalone modules.

For the past several years, I have been using a home-grown templating system that functioned by parsing template files which would have a dedicated / invented syntax. It would replace, for example, %TAG%, with a value. It had several more advanced features too, such as conditionals, recursion, embedded PHP, and more.

And while it functioned just fine, it implemented a custom parser, and required templates to be written using a non-standard syntax. But, after having used Ruby on Rails, I was inspired to design something better.

ATTemplate is the result of this. The main idea behind ATTemplate is that PHP in itself is already a parser, so why reinvent the wheel?

ATTemplate is very lightweight and relatively simple, but it’s very powerful without being bulky. It allows for everything the other templating systems can do, including loops, nested templates, recursion, conditionals, etc.

How to use

Here’s how you’d use ATTemplate:


// Include the ATTemplate class
require_once 'attemplate.inc.php';

$template = new ATTemplate();

// Optionally, you can give it properties, like so:
// $template = new ATTemplate($property_array);

$unread_messages = rand(0, 20);

// Sets property 'unread_messages' to 10.
$template->set('unread_messages', $unread_messages);

$template->set('title', 'ATTemplate Test');

$property_array = array(
    'first_name' => 'John',
    'time' => date('H:i:s'),
    'friend_requests' => array(
        array('name'=>'James', 'followers'=>rand(0, 200)),
        array('name'=>'Josh', 'followers'=>rand(0, 200)),
        array('name'=>'Aaron', 'followers'=>rand(0, 200)),
        array('name'=>'Eric', 'followers'=>rand(0, 200)),
        array('name'=>'Mike', 'followers'=>rand(0, 200))

// This pushes the content of $property_array into the properties

// Prints the parsed template
echo $template->parse('index.phtml')


And here’s what your template could look like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title><?php echo $title; ?></title>



<h1>Welcome, <?php echo $first_name; ?></h1>

<?php if($unread_messages>0): ?>
<h2>You have <?php echo $unread_messages; ?> unread messages!</h2>
<?php endif; ?>

<p>You have received friend requests from the following people:</p>
<?php foreach($friend_requests as $friend_request): ?>
    <li><?php echo $friend_request['name']; ?> (has <?php echo $friend_request['followers']; ?> followers)</li>
<?php endforeach; ?>

<p>The time is <?php echo $time; ?></p>



You’re free to use the code in whatever you want, commercial or not. Modify it, redistribute it, do whatever you want with it.

The only requirement is that you need to give me credit.

Also, please do shoot me an email if you use this in your project.

ATTemplate by Kenneth Ballenegger is licensed under the Azure License.


Download ATTemplate!

This entry was posted on Wednesday, July 1st, 2009 at 12:13 pm and is filed under English, Programming, Release, Web Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

1 Tweet

Add your thoughts!
kob says
July 1st, 2009 at 12:16 pm

Free PHP code: ATTemplate, an “agile” templating system. Does everything the others do, except better. http://bit.ly/attemplate

This comment was originally posted on Twitter

July 2nd, 2009 at 9:55 am

Propriety should be property. :-)

kenneth shrieks
July 2nd, 2009 at 10:11 am

Thanks for noticing. I had a feeling something was wrong… fixed!

Have something to say?
Name (required)
Mail (won't be published, required)

Additional comments powered by BackType