Review Site User Manual

ABOUT THE SMARTY TEMPLATE SYSTEM

Adjust Text:  a a a a
« Table of Contents   |   Obtain Review Site »


ABOUT THE SMARTY TEMPLATE SYSTEM

What Is It?

Smarty is a templating system written in PHP. It can therefore be used in PHP applications to allow customizable front ends (the public part of an application).

Let me say right off the bat that I don't think Smarty is a really great templating system. However, it seems to be the best one that is PHP compatible, and since the Review Site front end is written in PHP, that's the templating system I have chosen.

Basically Smarty is a relatively smart markup language that allows you to put tags on the page that get parsed and replaced with content from your database. Smarty allows you to pass data structures (accumulations of variables) to your templates. To keep life simple, suppose that Smarty passes only one data structure, and that it is named $s. This, in fact, is how Review Site operates. There is only ever one hash (data structure) and it is always named $s. The hash can, of course, be horrendously complex in structure, but in practice this is not the case.

As an example, let's suppose $s is passed to a particular template and it contains information about an article. The Smarty code that pulls out the article information from the hash, and displays it, might look like this:

[% $s.article_info.title %]
<br/><br/>
[% if $s.article_info.author %]
	Author: [% $s.article_info.author %]
	<br/><br/>
[% /if %]
[% $s.article_info.content|nl2br %]

In fact, that is pretty close to the actual code that is used in the article.tpl template to reproduce the content of an article.

The first thing you'll notice in this example is the use of if statements to decide whether or not a piece of HTML should be added to the page. The article title is a mandatory field for an Article record, so there is no need to check whether it exists. We can safely assume it does, and add the title to the page. Even if the article title was in fact empty, the information simply wouldn't appear on the page and we'd be none the wiser. Provided that you don't attempt to access a variable that doesn't exist, you won't see any Smarty-generated errors.

In the case of the value for the article's author name, this field is not mandatory in Review Site, so the field could well be empty. That's why a check is performed before adding the word "Author:" to the page. It would look out of place if it was presented but without an actual author name following it.

The article content is also mandatory, so it can also be assumed to exist. No need to check whether that field is populated.

You can see that the content of the $s data structure is keyed on variable names. In this case there is a variable named article_info which is another data structure containing all the information associated with a single article record. That data structure is keyed on the column names, so that [% $s.article_info.author %] corresponds to the value for the Article.author column.

Another thing you will notice from the example above is that the article content field has both a pipe symbol | and the string nl2br appended to it. This is an example of an applied Smarty filter, in this case to take the article content and replace all instances of multiple linebreaks with a pair of <br/> characters. This represents just one of the many built in features of the Smarty templating system.

Documentation

The only way to really get good at editing Smarty-enabled templates is to read the Smarty Documentation. So if you study the front end templates used in Review Site, and you don't understand how a particular feature works, go check out the Smarty documentation. Under no circumstances should you ask me why you don't understand how the templates work. The answer to that would be that you haven't taken the time to learn how Smarty works.

The one main difference between the Smarty code found in the Review Site templates, and the code found in the Smarty documentation pages, is that Review Site uses the tag delimiters [% and %] whereas Smarty developers tend to use { and } which I find makes templating code a lot harder to read. So make the mental substitution when you read through the Smarty documentation pages.

If you decide that you need something in your templates changed, and you can't figure out how to do it yourself, you should consider hiring a web developer to get it done for you. Unlike more advanced applications (such as Review Foundry) the templates in Review Site are relatively simple and any web developer who has any experience with Smarty ought to be able to follow the templating logic found in the Review Site templates.


Next Section: TEMPLATES

« Table of Contents   |   Obtain Review Site »


Copyright © 2008 Random Mouse Software. All Rights Reserved.