Using PHP Template Languages

One of the battles that seems to go on forever in the PHP community is whether to use a template language, in addition to, the PHP language.

The argument most heard is PHP is already a template language, why do I need another language?  And thus the debate begins.

The debate, I find, is one-sided.  Every comment I see of this nature is from PHP developers, and I suspect,  good PHP developers that develop entire web sites for their clients.  What I mean is they develop the entire site themselves.  If this is the case, why do you want a template language.  I agree.  The PHP developer develops the views, and everything behind it.  They understand PHP, so to put PHP in a view is a no-brainer.  They don't need a template language.

The problem with all the comments against template languages is that the only people reading articles on template languages are PHP developers.  And they're the one's making the comments.  Graphic designers don't read these articles or comments.  They read articles like how to resize a background image in an elastic web layout.

But, and here's where we start the other side, but if a graphic designer, or a team of graphic designers, is involved in developing a web site, then a template language makes some sense.

The company I'm working at has a team of graphic designers.  My impressions of graphic designers is they are interested in aestheic design, not PHP code.  Their personalities, are for the line of the art work, not a line of code. They like CSS.  They like moving an image 10 pixels to the right to line the image up with another image.  It gives them satisfaction.

That's why they're graphic designers.  They have an aversion for wanting to learn code.  You mention a line of code and their eyes glaze.

They like neat things, and neat looking HTML in their views.  Putting PHP in their views messes up their neatness.  PHP code in a view looks messy.  They stumble when trying to figure out what the line of code is doing, and how to test that in their design.

It seems to me that here is where a template language is desired, and why template languages for PHP are useful.

What makes for a good template language?

Let's start with the graphic designer's side.  It has to be easy to understand by graphic designers.  Simple concise  commands with very little template coding is needed.  PHP is not that.

It has to make their page look neat and orderly.  It should have HTML like tags with short concise syntax.  PHP is much more verbose, and sometimes takes many lines which messes up neatness.

The ability to test the design, without the real data using lorem ipsum blocks of text, for example. It should have a test mode to test designs before going live on the web site without necessarily having to have the PHP code in place.

It should support the creation of reusable templates.  It should allow a base template with small blocks of text templates throughout the site.

For the PHP developer side, a little more is needed from their template language.

It must be secure.  For PHP programmers to know a template language by default compiles secure code gives them peace of mind and saves time.  A web designer may know nothing about security when designing a page.

It must separate the developer from the designer.  The designer wants to change the page design without worrying about the code. The developer wants to change the code without worrying about messing up the designer's page.

It should support modern PHP programming, that is PHP 5 and  object oriented code.

It should be fast.  Your putting another layer on with a template language.  If the template language slows the site down, developers will want to go direct.

Finally, it should by easy to understand and learn.  The problem with template languages is they use another syntax, which has to be learned by both developers and graphic designers.  Interestingly, PHP developers balk at this.  I'm not sure why?  If the template is easy to learn both developers and designers won't mind using it.

What PHP template languages come closest to the above criteria?

The template engines that come closest to the above criteria, at this point in time, are: Smarty3DwooPHPTAL, and  Twig.

I will close with one other consideration.  Many developers use PHP frameworks when they develop.  Frameworks like: Symfony2, Kohana3 and Yii support reusable templates and reusable sub templates within views.  This speeds up the development of views and view files.  That doesn't mean that this precludes the use of a separate template language with the PHP framework.

It does sort of bring the debate to full circle though.  If I have the ability to have templates with a PHP framework, do I still want to use a template language for my graphic designers?   I think yes for the graphic designers in the crowd, but hey, if I'm a PHP developer, I might answer no.  What do you think?

Comments are closed.