Quite a few people now have started using and enjoying templating systems in PHP to more cleanly separate design and development. For those unfamiliar with the term, PHP templating is a system (like “Smarty”, one of the most popular for PHP) that allows programmers to divide application logic from content presentation. It uses tag-based mechanics to allow “tags” defined in HTML files to be replaced by data that is generated by PHP code behind the scenes. It makes for very nice and usable templating, as opposed to the sometimes-messy approach of blocking off sections of
<?php tags and having dozens of lines of code present, intermingling with HTML. Templating is a very attractive method of design that makes things look simple as well as highly readable, but it does have some drawbacks- here’s a list of pros and cons for templating vs. the normal PHP way!
- 1. It very clearly separates designers and developers. HTML/CSS designers can create the “skin” of your website, and they can make the layout of the website and just drop in tags for you to pass data to later, enabling designers with little to no PHP skill to rapidly design and deploy.
- 2. It makes PHP code extremely easy to read, and as a result of separating application logic from presentation it makes application logic easier to follow as well.
- 3. Entire code blocks can be completely overhauled with confidence that it won’t break the presentation- so long as the same variables are passed to the template, it will look just the same as it did before the code overhaul even happened.
- 1. Actually processing the templates can take a bit more time, as it is in the end additional overhead. It’s normally very minimal, but for applications that require every millisecond of time they can get it could make a difference.
- 2. The separation of design and development can benefit teams, but can also harm an individual: template designing can impact the workflow of an individual because of its focus on presentation. A programmer / designer can find his or her priorities shifting frequently between presentation and application logic at the detriment to both.
- 3. For existing projects, switching over to a templating system might be difficult and time-consuming. Large blocks of code would need to be rewritten to match the model of the template, and for some projects the effort and time would not justify the results.
So which ends up the winner? It’s hard to say in general- each specific scenario will favor one method over the other. Whether or not you use a PHP template like Smarty depends on how you need to get your work done- in general, however, you should definitely take a look at PHP templating when you’re looking to design your next application. The readability improvements as well as the ability to clearly separate design and presentation mean that your project could end up being easier to maintain and develop than if you had decided not to go down the PHP templating road!
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|Feb 16-17||Web Development with PHP/MySQL|
|Apr 20-21||Web Development with PHP/MySQL|
|- Classroom - Online|