Forum Moderators: coopster
So, I'm reading about smarty and it seems like a great method. (Write the application to feed the data, let the template engine display it.) But it also looks fairly complicated to put together.
Does anyone have any experience with using template engines and would you recommend using them?
Separating code and presentation makes it easier to think about what you're doing, and eliminate duplication. It's also a lot easier to make changes in either.
Some template engines also handle other things like caching. For instance, Smarty caches compiled templates, and with an opcode cache in PHP, it runs very fast. You can also enable caching of the output for even more speed.
All in all, highly recommended.
Sean
I think they are useless but let me explain why.
Templating engines are built so that everyone can use them, very generic and programmed for all eventualities. If you are rolling a ton of sites then sure they make life simpler, if you run one or two, forget it. Just make your own, I always make my own as each site is very different and has different requirements.
this thread might provide some insight also
Dynamic site in 2 minutes [webmasterworld.com]
You are much better off creating small 'modular' programs that can be integrated into any website and customised to suit.
;)
That being said, they aren't the most efficient tools, even something as nice as smarty. It's a trade off between code execution speed and your available development time, I guess you have to decide which is more valuable... for me, I'm always low on time so something like Smarty helps me speed things up and keep my clients happy.
... another argument for: If you are building something through which your client/visitors will need or want build their own visual experience of the site then using a templating engine that is easy to understand and has plenty of training material online will allow people who only have a little html knowledge to alter the visual appearance quite easily.
For something like that I'd never consider using PHP/Smarty, but a command line based one has saved me a lot of time.
Sean
What I'm thinking is if changing the layout of this site later is going to be a real headache maybe I should make the switch now while there are only 50 or so pages to deal with.
Then again I could always write a PHP script to copy a new dummy file to the same names as the existing pages, then it would not matter how many pages there are the changes would be handled by the server.
As I mentioned here:
[webmasterworld.com...]
I just don't get Smarty. I don't need Smarty to separate backend logic from presentation logic, I just need organized code (not that mine is, but why would Smarty help with that?), be it PHP, Scheme, or Smarty Templating Language.
I think Smarty can actually encourage convoluted presentation code in places where straight PHP would be clean, compact and elegant.
I see so many disadvantages, namely the one Timotheos mentioned - I need to learn a new language just to format my PHP data. If I'm going to learn a new language, it will likely be Python or Ruby, not the Smarty Templating Language.
I just don't get Smarty. I don't need Smarty to separate backend logic from presentation logic, I just need organized code (not that mine is, but why would Smarty help with that?), be it PHP, Scheme, or Smarty Templating Language.
Most programmer's won't need to seperate it, but there are a couple of cases where it comes in very handy. Imagine that your sites programmer and designer are 2 different people. The designer may not necessarily know how to code server-side, or may be proficient in another language (coldfusion,asp whatever). Something like smarty reduces the need for the developer to learn PHP before they can affect the sites visual appearance (For instance, how the template renders an array of data). It also reduces the learning curve of having to use another language.
Template engines also come in very handy when you have the need to compartmentalise code into model view controller patterns (usually for collaborative projects or web application frameworks).
I wouldn't advocate them for every occasion, but they most definitely aren't useless! The biggest bonus I can see for any template engine that seperates business logic from presentation logic is that it allows multiple people with different skill sets to collaborate on the one project without risk of cross contamination.
Unfortunately this approach needed me to check each article and add it to the site (the site was html-formattign them on the fly). Recently I've started thinking that perhaps a wiki would be the best development platform for my developers. Something like MediaWiki is well developed and has a rich feature set.
I could still not make hte ocntent live until my approval, but it would be a matter of a look and a click instead of a manual text editor review.
Has anybody here ever used a wiki purley as backend content development platform?
SN
kilroy, I've never played with MediaWiki but you might also want to check out some CMS packages like Drupal and Mambo over at [opensourcecms.com...] They have corroboration tools built in that just might fit your needs.
First off, I know what follows sounds argumentative, so I want to start by insisting that I'm not saying or even trying to say you're wrong, I just don't see it myself. I'll take your word for it that it's worked great for you. Maybe it's one of those things you have to commit to and learn before its utility is apparent, and I admitedly haven't done that.
It also reduces the learning curve of having to use another language.
This is the part that I don't get. Smarty, to all appearances, looks like another language to me, and one that's pretty hard to grasp. I've done somewhere between a medium amount and a tiny amount of work in: PHP, Scheme, C++, FORTRAN, Pascal, PDP-11 Assembler, CSS, HTML, VBA and Javascript. Smarty has hardly any overlap with any of those - even the syntax for something as simple as addressing multi-dimensional arrays is unlike that of any other language I've seen - so to me it's just one more thing to tackle that's non-intuitive (I'm not saying that PHP *is* intutitive, I don't think any programming or markup language is, Smarty included).
I just did a templated site that pretty much follows the Model-View-Controller pattern, though perhaps the objects should be a little more separate. In general, though, I like the MVC approach, I just haven't seen the big advantage that using Smarty offers and I have seen a disadvantage - namely instead of the HTML person learning a bit of PHP (which she can then use to do almost anything), the PHP person and the HTML person both have to learn a lot of Smarty (which can only be used for one thing).
I try to make templates so that they are very simple - mostly just variable substitution and perhaps a few methods to handle array output (as I think I already mentioned).
I think that at this level (variable substitution), PHP is no more difficult, and often simpler and cleaner than Smarty. Either way, the HTML person would have to learn a small bit of PHP or a a bit of the Smarty Language. If you look at that mess I got into trying to build nested lists with multi-dimensional arrays using Smarty (as linked above), I just don't see how that code is easier to follow or easier to learn than the much simpler PHP that would have rendered the same output.
PS. If you're wondering what Model-View-Controller means, check out the wikipedia article [en.wikipedia.org]
Has anybody here ever used a wiki purley as backend content development platform?
No, but I know of a great parser written as a PEAR package called 'Text_Wiki'. If you're not using it already, it's pretty cool.
The learning curve is much lower with Smarty than what you might think at first glance. It's not another language but rather provides a kind of wrapper to PHP and also provides a simpler interface to some higher level PHP functions. It is alot easier to learn than learning the syntax of PHP if only because it provides a very limited subset of functionality.
I'm not a smarty lover, but I enjoy a good discussion about seperating business logic from presentation logic, which is where I think smarty succeeds :). Take, for example, the Text_Wiki library I mentioned above. It's basically a class that grabs a piece of text and performs a few smart parsing routines and returns some nicely formatted xhtml. To use it in PHP you would have to do the following:
include ('/path/to/Text/Wiki.php');
$wiki = new Text_Wiki;
$text = '++ This is a test heading';
echo $wiki->transform($text);
If the developer wrote a quick plugin using smarty's plugin system the wiki transform method can become part of the presentation logic rather than mixed in with the rest of the code:
{$text¦wiki}
That tells the variable $text that has been assigned to the template by the developer to be handled as wiki text. All you need to do is remove the '¦wiki' part to stop it from being transformed (affecting the display logic). There's no need for the 'template designer' to know anything about how the wiki class operates, or include paths.
It's obviously not the ideal tool for every occasion, especially if you are the programmer/designer rolled into one. I think that having something (whether you write it yourself or use something like smarty) that seperates business from presentation is essential if you have the need to collaborate between programmer and designer.
The defence rests its case ;) hehe... I'm not being argumentitive, I'm just a nut for compartmentalising stuff. There are cases for and against, it just depends on the situation.
that's the thing that is so hard to get across a lot of the time, what's good for me isn't going to be good for you. What works on one site is a bad idea on another.
It always comes down to so many different factors, personal preference being high on the list.
In this particular case, whether people do or don't use out of the box templating engines, we all use something to do this for us. We all recognize the need but with all things PHP there's more than one way to skin a site. ;)