template engine choices (was Re: [PHP] template engine...why???)

From: Date: Thu, 01 May 2003 04:06:32 +0000
Subject: template engine choices (was Re: [PHP] template engine...why???)
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-145891@lists.php.net to get a copy of this message
Okay... now to take another shot at the question... It's not a question of whether to use templating or not. It's a question of what level of abstraction you want to use when templating. Using include() to insert a header and footer *is* templating. However, this approach has certain trade-offs. For instance, the placement of the include() relative to other generated screen elements is more rigid. However, it is an extremely simple form of templating that's easily implemented. From here, you could go to another level. You could generate a "template" which consists of nothing but calls to "presentation helper" code, as Rasmus suggests. In this case, you have a bit more flexbility, since you can possibly relocate includes() and calls to helper functions, as well as insert presentation elements directly. The trade-off is additional complexity because some of the presentation has been moved to a series of helper functions, where it is likely that presentation elements and code will be more mixed. If you want to go even more "abstract", you could attempt to separate *all* presentation elements into templates and template snippets, likely using some form of str_replace() to build the final presentation. In it's simpler incarnation, the formatting of the data itself (e.g. showing thousands separator in a number or not) will still be embedded in the code. More complex versions may allow additional control over the formatting from within the templating system itself. Whichever way you go, this approach adds more flexibility at the cost of complexity. In a sense, generating XML and running it through XSLT would be an example of this flexibility. As Rasmus points out, once you get to this level, you have practically re-created PHP (ever notice just how complex XSLT can get?). There are certainly other approaches that I have not covered. They may be hybrids of the above. They may be entirely different approaches. Regardless, they all have trade-offs that should be considered before using. Also, it is also important to note that some of the above approaches may have runtime performance constraints (notably the "abstract" version detailed above). In a sense, you are writing an interpreter in an interpreter (fortunately, the sabltron xslt processor is not written in PHP, or we would see just how slow an "interpreter written in an interpreter" could get). There are likely ways to optimize all of these approaches, one of which is to go through a "one-time" compilation process that takes the presentation and code and merges them into something that is resumably as free of "templating" overhead as you can get. Even in this case, there are trade-offs.... So, which way should you go? It depends on all sorts of factors: * Your comfort level with each style of templating. * What your application may require: * who will be editing the presentation portions * how complex the presentations will be * whether processing times will be significantly affected * etc. * etc. So... if include()s work for your needs, go with it! If you later on have a need for a different approach, go with it! As we have seen from this thread, there is no one right answer... --- Seairth Jacobs seairth@seairth.com "Ryan A" <ryan@jumac.com> wrote in message news:001501c30f66$c46bdd30$1301400a@l2zcaxu7emppqh... > > Agreed as well. :) So... did we answer the original question? I've lost > > track of what it was... > > > Let me help, I started this thread with a very simple question (or so i > thought). > Here the email below: > ************************************************* > Hi, > I'm a kind of newbie to PHP but have been with Java Servlets for years...I > would appreciate it if someone would answer this question for me please. > > Why use a template engine? > > isnt it much easier to include a header and footer file with "include"? > > eg: > <?php include "headerblah.php" ?> > > thats soooo much easier that learning some templating "language"...isnt it? > I visited the smarty template site..... > > Cheers, > -Ryan > ****************************************************************** > > Hope that brought back your memory :-) > > Cheers, > -Ryan >

« previous php.general (#145891) next »