Application Logic Versus Template Logic
This article takes a look at separating application logic from template logic. We will use the Smarty Template Engine as the basis for the article and its examples.
What this is basically saying is that anything relating to the final output should be determined in the template, while any data handling or processing should be done in your PHP files and classes. There’s a little more to it than though, which we’ll soon see.
An important aspect of this is that it works both ways. Often people focus on making sure their templates don’t do anything application related, but then make the mistake of making their PHP scripts (or classes) doing work that their templates should be doing. We will look at both situations and common issues that arise.
Application logic (also called business logic) is any code that deals with processing of data. That is, the processing behind the scenes. Common examples of this include:
- Processing the input from a web form
- Saving data to a database
- Debiting money from a credit card
- Reading or writing cookies on the client’s computer
We’ll look at some more examples of application logic and template logic shortly, for now, we’ll look at things that aren’t application logic and aren’t template logic.
Smarty allows developers to directly embed PHP code by using the {php} {/php} tag. Personally, while it’s good to have the functionality available, I think it’s almost always a bad idea to ever use this. It is also possible to use any declared PHP function (builtin or user defined) as a modifier (unless you have template security turned on).
This is extremely bad! You have essentially hardcoded in your application that your templates are built by displaying a header, then the main content, and then the footer. The fact that your templates are split into 3 templates like this is a function of the templates, not of the application.
In this situation, to deal with the fact a user might enter a quotation mark ("), we must escape the data. You could argue that this is application logic, but personally, I think this is directly related to the output of your HTML document, and so should be escaped when it is output.
Another exception I can think of would be if you wanted to list news articles in your template, but you’re not sure how many. That is, the number of articles shown is a function of the template. In one layout you might have room to fit three articles, but if you change templates, you might have room for five. So how many articles do you assign?
Personally, I think the second method is overkill and I would always use the first method. The additional problem is that we don’t necessarily know in the script what data the template requires.
The fact that we do something in the template for an empty array is display specific, not something that needs to be known in the application.
In this article I explained the difference between application logic and template logic when using Smarty. Hopefully you found some simple ways to improve your template design, but also recognised that some complexities can arise when doing this and sometimes comprimises must be made.
One (simplified) way to look at it is like this:
You should be able to completely change templates, including all image files and CSS files, without having to touch any of your PHP files.
All fundamental data required in your templates should be fetched or determined ahead of time and assigned to your templates (this doesn’t include meta data, such as the length of a string, etc.).