The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Smarty Templates - A Question

Im intrigued by the use of Smarty Templates for seperating data from design or structure from design but I'm wondering how many out there actually make use of them and are they in fact recommended or frowned upon.

Personally, I've come to the conclusion that templating is a good idea, but template engines such as Smarty and the like are missing the boat. They are essentially recreating PHP step-by-step (PHP started out as just a small templating engine).

PHP is still, at it's heart, a template engine. For small projects I use it as such quite frequently. My index.php is usually just a table skeleton (template) with includes for the various parts, eg, the header, the menu, the body, and the footer.

For smallish projects this works great, but I'm still searching for the perfect solution to medium to large size problems. Lately, I've been doing a lot of reading regarding MVC, Phrame, and related topics. That seems like a good approach but something still seems to be missing from it, I just can't quite put my finger on what it is yet. Most likely I just haven't read enough about it. However, this is the best overall schema I've seen. I just wish there was a quick and easy way to implement it.

I think sweatj (Jason Sweat) who is in this forum often wrote a really good piece on MVC in php|architect however I havent bothered to purchase the 2-3 previous issues that start the discussion on MVC. Have to con my wife out of the $3

The "Introduction to MVC in PHP" article was the headline article for the May issue, so it is available only if you pay for the issue. I covered the concepts behind MVC more than the specific use of Smarty in both of the articles.

I came across Smarty several years ago, and have never looked back. Prior to using PHP, I coded in ASP and had essentially Response.Write's (equivalent to echo) thoroughly mixed with database queries etc. It was a mess to look at and maintain.

Smarty's goal is not merely to separate your HTML code from your PHP, but to separate your application logic from your presentation logic. Application logic is essentially the "Model" portion of your MVC application. Anything to do with db/file manipulation and business logic of that interaction.

Presentation logic deals with how that data is presented.

I think the best way to understand the benefit of adopting a template engine like Smarty is to consider your application from two different perspectives: a developer (writing PHP) and a designer (HTML) (Note: in most cases in my projects, I am both, but I still like to wear, and think, both hats separately ).

The designer could care less where an array of data they are supposed to display came from. The developer could care less that the designer want to display the array in a table of three columns with colors alternating on each row. By throwing Smarty into the mix, it allows each role to apply logic within the domain that they are working without interfering with the other domain.

Smarty has the capability to carry this to the extreme with a "safe mode", where you could conceivably allow your users to upload templates for use on your web server. By severely limiting what is allowed in the templates, this choice would not be suicide for your server security

Another complaint I have seen regarding the use of "feature rich" (or "bloated" depending on your perspective ) template engines is the overhead involved. Smarty has addressed this issue in two ways. The first is it's overall implementation, rather than being a simple regex replacement engine, it has a "compiling" stage that will evaluate a template each time it has changed and produces a "compiled" template which requires much less overhead to execute on future passes. The second means that has been adopted to improve performance is a modular design, with a minimal base class that included addition resources as they are used in templates.

All of that being said, I think the uses of templates and template engines is going to boil down to personal preference. I view the Smarty syntax as a "macro language" for HTML, not a "step by step replacement of PHP", and I have very comfortable with its use in my own "designer" role. I would encourage you to try Smarty out on a project of your own and make your own decision regarding its merit.

Regards.

P.S. on HarryF's recommendation (http://www.phppatterns.com/index.php...leview/29/1/9/) I spend $18 for a years subscription in January and I have been very happy with the quality and content of the magazine personally. As long as you have to talk to the wife on the issue, try to get the annual subscription out of her