What is SmartTemplate?SmartTemplate is a Template Engine that was designed to support large scale, web based applications.
What's so special about it?
Common template engines work like the following: Your PHP script specifies an HTML template and assigns some dynamic content to display. The template parser replaces all placeholders within the template with the assigned content and displays it to the user. This means a lot of string processing and regular expression work each time you want to display some content.
SmartTemplate works like a 'template compiler' that converts templates into executable PHP code and stores it for later reuse. The first time a new template is processed, all placeholders in the template are replaced by small PHP code elements that print the assigned content. The HTML template fragment <H3>{TITLE}</H3>, for example, is converted into something like <H3><?php echo $TITLE; ?></H3>. If you have assigned your content to the right variables, there is no need for any template parsing anymore. The only thing that has to be done is to include and execute the compiled template. This usually increases the execution time of the template engine dramatically.
SmartTemplate supports:
• Simple Scalar Substitution (Strings, etc.)
• Block Iterations (nested Arrays / BEGIN..END)
• Basic Control Structures (IF..ELSEIF..ELSE)
• Custom Extension (Output filters, uppercase, sprintf, etc.)
• Template Compilation (HTML templates are converted to executable PHP Code)
• Output Caching (Accelerates your applications by reusing page output)
Let's start with an easy example to show how to use SmartTemplate:
Usually the process of creating a webpage begins with a preview of the future result. Creating simple layouts this can be done using a webeditor like Dreamweaver or Homesite. For more complex page layouts one should always consider using a professional design tool like Photoshop, Paintshop Pro, Gimp etc.
Preview: Hello World!
Now we create the HTML source that generates a web page of our desired design:
Now the exciting step: The separation of content and design. We replace all dynamic content elements in the HTML-scource by placeholders. They get a unique name and an own tag around them so we can identify them later. The title Hello World! is replaced by a placeholder named TITLE. We mark this placeholder by embedding it in curly brackets, so the template parser can find it: {TITLE}. The HTML Template now looks like the following:
To populate this template with content we have to:
Load the SmartTemplate Class
Instantiate a SmartTemplate Parser Object
Tell SmartTemplate which HTML-Template to use
Assign the content to it's associated placeholders
Process Template
Print result
The following PHP Script will do the required steps:
hello_world.php: <?php
require_once "class.smarttemplate.php";
$page = new SmartTemplate("hello_world.tpl.html");
$page->assign('TITLE', 'Hello World!');
$page->output();
?>
That's all - our Hello World example is ready to run.