Don't be Stupid, be a Smarty

My greatest irritation with PHP, and the one that kept me away, is its muddle of control and presentation. The program logic and HTML display often are jumbled together, where long piles of code squirt out blobs of HTML in random places. This creates a disaster for program structure and a nightmare for system maintainability.

For months, I've been wanting to add a registration system to the Austin Bloggers web site. Every time I thought about it and what would be involved, my stomach would churn. Then, I remembered some time back Matt Sanders telling me about this template engine called Smarty. I thought I'd look into it.

The Smarty system goes a long way to addressing this problem. By using Smarty you can almost completely separate logic and presentation. In fact, with the addition of a message catalog library for sundry diagnostics and notices, it wouldn't be too tough to create a completely internationalized web application.

I'm pleased with the speed of development when using Smarty for web applications. Yesterday, I created an entire application (a portal for journal entry postings) in an afternoon. The application, although simple, has some pretty sophisticated features. For instance, it was easy to setup fancy form validation. If you make an error the form is returned to you, the cursor is placed in the problem field, and a detailed diagnostic message is displayed there.

The debugging console is an amazing feature. It is easy to turn on during testing, and it gives a full view of the data hidden behind the web page.

There are two areas I'm less happy with. One is the mechanism for passing field values through functions. It's very nice you can process data that way, even through predefined or custom PHP subroutines, but the syntax is very awkward. Also, the mechanism for HTML quoting is tedious. In my applications, nearly every instantiation of a field value should have |escape:"html" added (There is that awkward function syntax again.) I find myself often forgetting to do so. I wish there was a way to make that the default, and then do |escape:"none" in those few places where the value actually contains HTML content.

I still think web application programming is a tedious exercise. Thanks to Smarty, however, it becomes a lot more tolerable and I get finished a lot quicker.


Comments have been closed for this entry.

re: Don't be Stupid, be a Smarty

I'm curious, what method did you use to do your form validation?
One method I've used is to create an array. Each field in the form is tested for validity. If a field flunks the test, the field name is added to the array as a key with the value being the appropriate method. Once all checks are complete, the program tests the contents of the array, if it is larger than 0 (zero), the form is displayed along with the error message.