Archive for September, 2006

Zend_Service_Currency Proposal

Since one of the project’s I’ll be working on in a few months requires using up to date Exchange Rates (without the benefit of having a paid-for Bloomberg/IDC/Other terminal available) I did a quick check on what PHP classes are available for the purpose. There’s really not a whole lot out there. There’s a PEAR class along the lines I need, but it’s released under the PHP License and, not to be too argumentative, I prefer non-PEAR solutions where the end-user may have no access to update PEAR.
So since the project is planned to use the Zend Framework, I shot off a mail to the Framework’s fw-general mailing list to see if it would be a worthwhile contribution. As blind luck would have it, someone else had just then finished opening a proposal for a Zend_Service_Currency addition (the text has yet to be added). André Hoffmann (the guy who jumped first) was open to collaboration, so I’ve leaped on board as a second set of hands and eyes.
For those remotely interested, the draft proposal I forwarded to the mailing list as a discussion point (mainly for André) can be viewed from: The basic premise is simple - the class will be capable of fetching exchange rates between any two currencies from a selection of sources. For the moment that means the European Central Bank and US Federal Reserve. Other sources will get added eventually, and the ability to add more supported by subclassing either as part of ZF or by addition by individual developers.
Eventual functionality includes spot fx rates, currency conversion, and fx rate list fetching from the selected source. It will likely use the proposed Zend_Currency class to handle locale issues (the usual comma vs period debate between Euopean mainlanders and the UK/US folk) and ensure currency codes are ISO 4217 compliant. Zend_Cache will be used to cache fetched data for a set period of time.
All I need now if for some higher power to review the CLA I forwarded back when and clear me as a contributor!

Template Lite: A Sweeter Smarty Alternative

Use the term Template Engine in a PHP forum and the word "Smarty" will inevitably crop up. Smarty is a PHP Template Engine, a library which parses a text file containing Smarty markup and outputs the parsed content as HTML, XML or any other text content you prefer. I like Smarty but, similar to a few PHP libraries, it’s very bloated.

Now I could start throwing the "scalable" buzzword so I’ll immediately limit my post’s scope to concerns on a single server where hardware expansion is not a realistic option. This impacts shared hosts obviously as well as standalone dedicated servers.

Such resources generally have limitations - memory, CPU cycles, I/O vs Database latency, etc. For some people (me!) these are a concern. Faced with resource limits I try to keep my applications lean and mean - small, fast, and adaptable. Unfortunately, Smarty disagrees with my needs. It’s bloated design while packing a mighty punch, uses a chunk of memory and processor time I dislike. Since I also develop open source apps which end up on shared hosts, my concerns are even more justified.

Enter Mark Dickenson. Mark is the developer of a Smarty alternative: Template Lite. Template Lite is not simply another template engine. It’s objective is to maintain Smarty compliance (i.e. it parses Smarty markup) using a redesigned library for minimal memory and CPU cycle consumption. Not happy with just that, Mark has also introduced some non-standard Smarty markup which, simply enough, provides more intuitive methods for traversing arrays, looping, etc. - for example, the SWITCH, FOR and WHILE tags. The results? A Smarty drop-in replacement which is limited to approx. 50% of Smarty‘s usual memory usage and is significantly faster at compiling and displaying templates.

So why use Template Lite? Template Lite is likely not everyone’s cup of tea. It’s an excellent library which is small, readable and surprisingly simple to extend. It’s fast and has approx. 50% of Smarty’s typical memory usage. It’s extendable, and contains additional tags which are more intuitive (to me at least) since they follow programming conventions common to PHP, C++, etc. On the flipside, Template Lite excludes several Smarty features. Use of Template Lite likely depends on how much weight programmers place on these elements. The main one I’ve seen discussed is that Template Lite excludes the "Security" features of Smarty. These place limits on template content and locations which are useful if Templates are editable by untrusted third parties.  Technically, this optional behaviour is unnecessary for most purposes (it’s disabled by default in Smarty), but may be essential for some programmers who want to place limits on in-house designers or such. The include_php tag has also been omitted, and error reporting is not very in-depth.

Overall however, I found Template Lite to be a useful addition to the PHP Toolbox. In circumstances where Smarty appears too heavy handed, Template Lite offers a faster alternative. It’s cleaner design and modularity are the icing on the cake. Template Lite 1.90 was the latest version released on 1 September 2006 - if you haven’t already, check it out.