Do you know why PHP was initially created? I am sure you do, but many programmers have forgotten. The first purpose of PHP was to create simple dynamic web pages.
Who knows why (seriously, do you know WHY?) groups of PHP developers keep working on PHP template engines which are supposed to help separating the program logic from the design. One good sample of such useless effort is Smarty.
So Why Smarty Is Evil?
What exactly is Smarty and the similar template engines doing and why is it so bad for your programs to use them? Let me just shoot few reasons:
- Adding one more language to learn. Smarty has its own language which has to be used in the templates whenever you need control flow structures etc. How exactly is this better than using PHP itself?
- Putting presentation code in your logic. They claim to avoid that and at the same time require you to “load” all the variables before you can use them in the template. The absurdic $smarty->assign may take more lines in your code than you need for the program logic.
It’s hard to believe, but most programmers would visit Why Use Smarty page and take everything said there for real. However I prefer to think myself:
“Designers can’t break application code. They can mess with the templates all they want, but the code stays intact. The code will be tighter, more secure and easier to maintain.”
– Really? Designers can break the application code, because the templates (view layer if you use MVC) contain logic themselves. In no way the template engines protect you from that
“With presentation on its own layer, designers can modify or completely redesign it from scratch, all without intervention from the programmer”
– Big fat lie. The programmer needs to load all variables, prepare arrays and do presentation related work in the controller layer. The presentation is not on its own layer so the designers often need the programmer’s help.
“Programmers aren’t messing with templates. They can go about maintaining the application code, changing the way content is acquired, making new business rules, etc. without disturbing the presentation layer.”
– See all the points aboe. The programmers do mess with the templates.
“Templates are a close representation of what the final output will be, which is an intuitive approach. Designers don’t care how the content got to the template. If you have extraneous data in the template such as an SQL statement, this opens the risk of breaking application code by accidental deletion or alteration by the designer.”
– If the programmer puts SQL statement in their view layer, then they simply need some education. Using simple PHP templates you can and should keep all the logic away from the presentation layer. You don’t need Smarty, Fast templates or similar misconceptions.
So How Do You Go Without Template Engines?
PHP is template language itself. You don’t need another one. Just separate your logic from your presentation. Keep your logic in a php script, then your presentation in HTML template. When you need control flow in the HTML file, just use pieces of PHP code.Â It’s really as simple as that. In your PHP script you can use includeÂ or require to display the template. All the variables which are in the scope of the script will be available in the template as well. No loading required, no messy presentation code in the controller layer.
If you need samples, just let me know (hint, use the comment option).
Filed under: Technology Made Simple