Go Back   Work From Home Forum -- Work At Home -- Working From Home > Insider Secrets Club Private > Programming Talk

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 03-02-2009, 12:39 PM
wishingSTAR's Avatar
Just starting out
 
Join Date: Dec 2008
Posts: 47
Thanks: 5
Thanked 0 Times in 0 Posts
Rep Power: 0
wishingSTAR is an unknown quantity at this point
Default Beyond the Template Engine (article)

In general, template engines are a "good thing."

I say this as a long time PHP/Perl programmer, user of many template engines (fastTemplate, Smarty, Perl's HTML::Template), and as author of my own, bTemplate.

However, after some long discussions with a co-worker, I've decided that the vast majority of template engines (including my own) simply have it wrong. I think the one exception to this rule would be Smarty, although I think it's simply too big, and considering the rest of this article, pretty pointless. There are, however, a couple of reasons why you might choose Smarty (or a similar solution), which will be explored later in this article.

This article discusses template theory. We’ll see why most "template engines" are overkill, and finally, we’ll review a lightweight, lightning fast alternative.

Download And Licensing
The template class and all the examples used in this article can be downloaded here: template.zip. You may use the code in these files according to the MIT Open Source License as published on OSI.

A Little Background on Template Engines
Let's first delve into the background of template engines. Template engines were designed to allow the separation of business logic (for example, retrieving data from a database or calculating shipping costs) from the presentation of data. Template engines solved two major problems:


How to achieve this separation

How to separate "complex" php code from the HTML

This, in theory, allows HTML designers with no PHP experience to modify the appearance of the site without having to look at any PHP code.

However, template systems have also introduced some complexities. First, we now have one "page" built from multiple files. Typically, you might have the main PHP page responsible for business logic, an outer "layout" template that renders the overall layout of the site, an inner content-specific template, a database abstraction layer, and the template engine itself (which may or may not be comprised of multiple files). Alternatively, some people simply include "header" and "footer" files at the start and end of each PHP page.

This is an incredible number of files to generate a single page. Yet, as the PHP parser is pretty fast, the number of files used is probably not important unless your site gets insane amounts of traffic.
However, keep in mind that template systems introduce yet another level of processing. Not only do the template files have to be included, they also have to be parsed (depending on the template system, this can happen in a range of ways -- using regular expressions, str_replaces, compiling, lexical parsing, etc.). This is why template benchmarking became popular: because template engines use a variety of different methods to parse data, some of which are faster than others (also, some template engines offer more features than others).

Template Engine Basics
Basically, template engines utilize a scripting language (PHP) written in C. Inside this embedded scripting language, you have another pseudo-scripting language (whatever tags your template engine supports). Some offer simple variable interpolation and loops. Others offer conditionals and nested loops. Still others (Smarty, at least) offer an interface into a large subset of PHP, as well as a caching layer.

Why do I think Smarty is closest to right? Because Smarty's goal is "the separation of business logic from presentation," not "the separation of PHP code from HTML code." While this seems like a small distinction, it is one that's very important. The ultimate goal of any template engine shouldn't really be to remove all logic from HTML. It should be to separate presentation logic from business logic.

There are plenty of cases where you simply need logic to display your data correctly. For instance, say your business logic is to retrieve a list of users in your database. Your presentation logic would be to display the user list in 3 columns. It would be silly to modify the user list function to return 3 arrays. After all, this function shouldn't be concerned with what's going to happen to the data. Yet, without some sort of logic in your template file, that's exactly what you’d have to do.

While Smarty gets it right in that sense (allowing you to harness pretty much every aspect of PHP), there are still some problems. Basically, it just provides an interface to PHP with new syntax. Stated like that, it seems sort of silly. Is it actually simpler to write {foreach --args} than <? foreach --args ?>? If you do think it's simpler, ask yourself whether it’s so much simpler that you can see real value in including a huge template library to achieve that separation. Granted, Smarty offers many other great features, but it seems like the same benefits could be gained without the huge overhead involved in including the Smarty class libraries.
Reply With Quote
  #2  
Old 03-02-2009, 05:58 PM
amyd74's Avatar
Getting good at this!
 
Join Date: Sep 2008
Posts: 53
Thanks: 8
Thanked 3 Times in 2 Posts
Rep Power: 26
amyd74 is an unknown quantity at this point
Default

It's a good article, and it makes some good points about the usefulness of some popular templating systems, including the point that PHP itself makes a good template system, eliminating the need to learn another syntax.
Reply With Quote
  #3  
Old 03-03-2009, 09:32 AM
SheRocks's Avatar
Getting good at this!
 
Join Date: Jan 2009
Posts: 82
Thanks: 5
Thanked 2 Times in 2 Posts
Rep Power: 24
SheRocks is an unknown quantity at this point
Default

Thank you... You explained why Smarty and the other templates are so hard to sort out. Your artical was written so that I could understand the process.
Thanks so very much..
Reply With Quote
  #4  
Old 03-03-2009, 08:01 PM
LivingLARGE's Avatar
Getting good at this!
 
Join Date: Jan 2009
Posts: 58
Thanks: 6
Thanked 2 Times in 2 Posts
Rep Power: 24
LivingLARGE is an unknown quantity at this point
Default

I liked this article a lot, specially since it gives me good hope of creating a nice template system without learning another language, just a smart way of using PHP. I still have to learn classes though, but was gonna anyway
Reply With Quote
Reply
Work From Home Forum -- Work At Home -- Working From Home > Insider Secrets Club Private > Programming Talk

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Engine Optimization (SEO) Using Keywords and Phrases kentuckyslone Home Business Talk 12 01-21-2009 12:41 AM
How To Use Article Marketing To Benefit Your Business tam2005 Everything Marketing 0 02-21-2008 11:44 AM


All times are GMT -6. The time now is 01:58 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2
Talk Home Business

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28