Smarty templates for classifieds software

They have: 1 posts

Joined: Oct 2006

Hello everybody,

Need your opinion on the Smarty template engine. We’ve included it in our classifieds software packages [links removed] as a part of template management system. Do you think if Smarty is an advantage in template management? Are there any pitfalls in using it?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

I'm afraid I'm not much of a Smarty fan... Wink

Yes, there are pitfalls. Smarty itself falls into a trap of reinventing wheels that, I think, domain-specific languages often do.

The motivation for Smarty is fine: to separate application and presentation. However, I don't see how that implies reimplementing a whole language -- albeit a restricted (but growing!) one.

To cope with presentation logic, Smarty has reimplemented control structures like conditional branching, looping and so on, which belong to a general-purpose language. In this case that language is PHP, already at hand where Smarty is. Therefore Smarty becomes redundant, and adds unnecessary complication. Sad

PHP can be used for both application and presentation logic, even to make templates. Along with otherwise good systems architecture, one merely needs to keep business logic out of the presentation layer, which requires a little discipline.

Ironically, even Smarty has the same challenge:

Quote: This doesn't mean that Smarty prevents you from putting application logic in the template, you have to have a bit of self discipline.
http://smarty.php.net/whyuse.php

I think all the benefits of Smarty are consequent to the general good practices that it encourages; and not a result of ingenuity in the Smarty implementation itself. As well as PHP, even XML/XSLT -- if you are so inclined -- can be used in a three-tier MVC-like approach, for instance. Smiling

One example of a simpler presentation layer that doesn't reinvent the wheel is phptemplate for Drupal. While I admit that a presentation language embedded -- rather than only implemented -- in PHP would be preferable even to phptemplate, that wouldn't be a realistic expectation given PHP's limitations. Smiling

So Smarty is a great idea, but I wouldn't use Smarty to practise it. Wink

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Have to agree with Abhi here. In plain, non-software engineering terms: it's overcomplicated. By the time you've learnt how to use Smarty you might as well have just written everything in PHP.

What alternatives are there to Smarty Abhi? I know about phptemplate, but that only works for Drupal (?).

a Padded Cell our articles site!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

JeevesBond;208603 wrote: What alternatives are there to Smarty Abhi? I know about phptemplate, but that only works for Drupal (?).

I don't think you need an alternative package as such. You can put the template idea to practice with an appropriate system architecture in your application. Which to a large extent amounts to avoiding directly calling business routines or writing data from the template.

A Model-View-Controller approach often works out reasonably well. Have a look at Rasmus Lerdorf's toy no-framework PHP MVC framework, a neat example of keeping it simple. While I don't wholly like Marston's version of PHP MVC that I linked to in the last post (it's kind of large), it also shows the same basic point that Lerdorf's does:

Rasmus Lerdorf wrote: Nothing is going to build your application for you, no matter what it promises. ... In the end I think you will find that your homegrown small framework has saved you time and aggravation and you end up with a better product.

In practice, all this matters only for web applications that aren't maintainable without a separate presentation system. Smaller sites can frequently get away with far less effort expended in architecture. Smiling

(... as far as PHP goes, anyway.)

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.