Forum Moderators: coopster

Message Too Old, No Replies

Template Engines, good or bad?

         

too much information

2:24 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just found template engines today and am starting to realize just how little I know about what can be done online. And here I thought I was pretty high on the knowledge ladder...

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?

SeanW

2:44 pm on May 17, 2005 (gmt 0)

10+ Year Member



I used Perl and Template Toolkit for the longest time. I've since moved some of my stuff to PHP to open up my hosting options, and have been using Smarty. I looked at a few before making the decision, and Smarty seemed to be the one that I felt most familiar with.

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

jatar_k

4:18 pm on May 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'll go the the other way

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]

dcrombie

5:24 pm on May 17, 2005 (gmt 0)



I'm with jatar_k on this. Every PHP programmer after a couple of years thinks that they can create the be-all-and-end-all of template systems. Some get further than others.

You are much better off creating small 'modular' programs that can be integrated into any website and customised to suit.

;)

jatar_k

5:25 pm on May 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> after a couple of years thinks that they can create the be-all-and-end-all

then after a couple mroe we realize we might know less than when we started ;)

killroy

11:29 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do that anew for each site. Each time I think if I put in a bit of extra effort I can finally reuse that engine for all future sites. But each tiem I learn more so that I start over on the next project again.

SN

mafew

12:24 am on May 18, 2005 (gmt 0)

10+ Year Member



I use Smarty and love it.

I dread updating sites that I made before I started using Smarty.

Smarty's very flexible and easy to use.

I create my own CMSs which implement Smarty.

I highly reccommend it.

ironik

2:49 am on May 18, 2005 (gmt 0)

10+ Year Member



Ditto on Smarty, and it's not just a templating engine, it helps you get all the presentation logic seperated from the business logic. It also has some nice plugin and caching abilities, as mentioned above.

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.

SeanW

12:49 pm on May 18, 2005 (gmt 0)

10+ Year Member



On the small site topic, I still found templates to be helpful. I have a few small sites that I use Template Toolkit's command line tools to work with. I have a directory of a couple of templates that define how the site looks, and then the content in another template. When I make a change to the content, I run a script that rebuilts the site and uploads the HTML. This lets me push changes to several pages with only one change.

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

too much information

12:53 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the link jatar_k, but the last site I set up that way ended up having more files than I could manage by my self. The newer version is completely database driven so every page is just a copy of a dummy file with an included "fill_content.php" file that feeds the content depending on what page is being called. It's my own version of a template engine before I knew there was such a thing.

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.

jatar_k

4:02 pm on May 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> Then again I could always write a PHP script to copy a new dummy file

hehe, that's where I always end up, I can always write a quick script (php, shell, perl) that can add that little extra something I need instead of moving the whole site over to new technology.

Timotheos

4:23 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See my msg #13 here [webmasterworld.com...]

ergophobe

1:01 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Another dissenter...

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.

ironik

3:12 am on May 19, 2005 (gmt 0)

10+ Year Member




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.

killroy

9:27 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On a slightly different track. I'm working on a few projects were I have several outsourced content writers who add content at tehir own leisure (writing new articles and so on). I used my own, simplified wiki variant to let them do basic formatting and on page SEO without learning anything complicated.

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

Timotheos

3:26 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very good points ironik. I'm sure glad open source projects (like drupal, etc.) use templates. It makes it sooo much easier to tweak the look.

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.

ergophobe

3:28 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ironik,

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]

ironik

10:25 pm on May 19, 2005 (gmt 0)

10+ Year Member




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.

jatar_k

10:30 pm on May 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> 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. ;)