Template Engine: An Overview of Smarty Templates & Other Comparisons

The template engine‘s job is to help separate out application model logic from display logic and to provide a simpler, more readable way to add functionality to your display files. Since HTML was never meant to handle arbitrary logic, it is generally kept separate from the Model and Controller layers of an application – learn more about MVC.
Template Engine: An Overview of Smarty Templates & Other  Comparisons

However, it’s inevitable that at least some presentation logic gets mixed up into the HTML template file due to requirements such as “Alternate background color of each table row” or “Show only positive balances”. So, rather than mix standard PHP code into the HTML template file, template enginhowes were conceived which mask common PHP functions and tasks with more presentable and concise code and allow for a deeper separation of logic and markup.

Standard HTML Template vs Template Code

So while a standard HTML template might contain code like this:

1 <?php foreach ($items as $item): ?>
2 <div><?php echo $item; ?></div>
3 <?php endforeach; ?>

Template code might look more like this:

1 {foreach from=$myArray item=foo}
2 <div>{$foo}</div>
3 {/foreach}

As you can see, the latter would be much more readable by non-coders and looks more like typical markup than PHP code. But although the code may be prettier, one of the criticisms for using a template engine is the extra resources and time spent compiling the template code into actual PHP code. This really is a give and take. If you are a sole developer working on a project and you don’t really foresee having more than a few developers working on the project at a time, then maybe a template engine isn’t for you. On the flip side, if you end up having multiple developers and front end coders editing your code later on, a template engine could be a huge benefit.

While there are many template engines out there, some stand out among the others. Smarty Template Engine is one of the older engines that has been scrutinized and improved upon year after year. Because of its age, there is a wealth of resources for solving Smarty problems across the net.

See full article:

Enhanced by Zemanta