PHP Template Engines?
kubed asks: "I've recently learned how to use PHP template engines to separate business logic from presentation. Some argue that template engines make applications easier to maintain and make for cleaner code. Others argue that template engines introduce unnecessary overhead and require too much additional processing power. Do the readers of Slashdot think that it is important to use templates or are they just an extra unnecessary layer? There are dozens of PHP template engines to choose from including Smarty, phplib, and bTemplate. Which template engines do you have experience with and which ones have the best performance?"
I use XHTML 1.0 Strict and CSS extensively, so when it comes down to it, there is very little in the way of markup to abstract away, and a template engine merely gets in the way.
If you are constantly tripping over nested tables and font elements, perhaps you are solving the wrong problem by trying to rationalise that markup instead of using HTML properly in the first place.
Given the apparent popularity of templating systems for PHP I'm likely in a minority, but I really don't see the point. Languages like perl need some way of getting variables and at least some basic controls structures into the HTML so you don't have to resort to multiple print statements (god, the bad old days...). But PHP does this all on it's own already.
Granted, this is often horribly, horribly abused with all kinds of spaghetti code strewn about the presentation layer, but this is the developer's fault, not that of the language. There's absolutely no reason you can't implement an MVC architecture (or just put your main code somewhere other than your presentation layer) without resorting to a templating engine.
As far as I can tell, the only benefit of PHP templates is that it forces you to keep your code somewhere other than in the presentation. This is offset by generally having the ability to drop out into PHP anywhere you want to in the template anyway. In exchange, you add another layer of complexity to your application, increase execution time, are forced to learn a new syntax and are (frequently) shoehorned into the way the template engine thinks you should structure things.
It's also often mentioned that it's easier on non-coders if you're handing the templates off to someone else for markup. But I really don't understand why (excuse the lack of indintenting - slashdot didn't like it)
is considered more intuitive than:And if you're going to expose your HTML people to a tiny bit of code, it might as well be the actual language, which they may find useful someday. (Yeah, there's a couple more lines in the PHP example to suit my own formatting tastes).
It seems to me that their only real purpose is to help enforce some kind of coding standards. I prefer to excercise a little discipline on my own. Nothing but variable expansion and control structures go into the presentation layer. The code that does the real work is elsewhere. If I'm overseeing others, I make sure they do the same. And god help them if they use a print statement for anything besides debugging.
(Caching comes up as an advantage on occasion, but there are other options that don't involve marrying yourself to a template engine).
I'll grant that I might be missing something obvious and wonderful. If I am, this is the place it'll be pointed out...
This is the voice of World Control. I bring you Peace.
Granted, any language can be misused to create horrible code, but with rapid development with template languages, you have to be careful not to let the ease and simplicity of the language lull you into poor programming.
In my experience, bad programming starts with the developer, not with the language. Languages that are more difficult to code in simply make the barrier to understanding a little higher, so they're *more likely* to be used by someone with good programming skills.
Remember: The main goal of programming is to ensure that the end result solves the problem presented. Elegant code that doesn't result in what you want is probably worse than slightly ugly looking code that actually works.
I've encountered plenty of programmers who are skilled at coding, but aren't good at paying attention to the needs of the project, so they spend time on the wrong elements, implement features that aren't needed, and omit features that are needed.
The advantage of more flexible coding (which is equally possible with ColdFusion as it is with a more complex language) is that it is faster to make changes to the system. If everything was hardcoded or overly-coupled, then changes will be difficult. However, for well coded ColdFusion applications, changes are very, very easy to make -- much easier than a similar implementation in a "real" language.
I've been coding in ColdFusion for a long time and I won't claim that I've never written bad code, but the language itself allows for applications that are very well written, robust, fast, and easy to update and change.
Fusebox is the most popular programming methodology for ColdFusion, and they have versions of it for PHP as well now. I'm not sure how well it works with PHP, but I do know that it does a fantastic job with ColdFusion.
Karma: Chevy Kavalierma.
No, not really. I mean, from which end? Your .php files sure aren't going to be cleaner.
Frankly, I think you're missing the point. CSS and templating are not mutually exclusive. Just as CSS helps us separate style from content, so does templating help us remove content from application logic.
3) With templates and CSS
I prefer #3.