Monday, June 12, 2006

Smarty Classes

Smarty classes are PHP classes which have one basic purpose, "To separate the business logic and presentation logic". Using smarty classes, we can write separate code for the business logic in PHP. Usually presentation logic is written in HTML which is echoed from PHP. How many times have we wished that we can use PHP in the presentation logic part to obtain more control over how the data is displayed? Smarty is the appropriate answer to this, for it enables to write embedded PHP code in the HTML level, with all the frills of HTML along with the control logic of PHP

Though Smarty is known as a "Template Engine", it would be more accurately described as a "Template/Presentation Framework". That is, it provides the programmer and template designer with a wealth of tools to automate tasks commonly dealt with at the presentation layer of an application. I stress the word Framework because Smarty is not a simple tag-replacing template engine. Although it can be used for such a simple purpose, its focus is on quick and painless development and deployment of your application, while maintaining high-performance, scalability, security and future growth.

Some of the exciting features of Smarty include:

Caching: Smarty provides fine-grained caching features for caching all or parts of a rendered web page, or leaving parts uncached. Programmers can register template functions as cacheable or non-cacheable, group cached pages into logical units for easier management, etc.

Configuration Files: Smarty can assign variables pulled from configuration files. Template designers can maintain values common to several templates in one location without intervention from the programmer, and config variables can easily be shared between the programming and presentation portions of the application.

Security: Templates do not contain PHP code. Therefore, a template designer is not unleashed with the full power of PHP, but only the subset of functionality made available to them from the programmer (application code.)

Variable Modifiers: The content of assigned variables can easily be adjusted at display-time with modifiers, such as displaying in all upper-case, html-escaped, formatting dates, truncating text blocks, adding spaces between characters, etc. Again, this is accomplished with no intervention from the programmer.

Template Functions: Many functions are available to the template designer to handle tasks such as generating HTML code segments (dropdowns, tables, pop-ups, etc.), displaying content from other templates in-line, looping over arrays of content, formatting text for e-mail output, cycling though colors, etc.

Filters: The programmer has complete control of template output and compiled template content with pre-filters, post-filters and output-filters.

Resources: Templates can be pulled from any number of sources by creating new resource handlers, then using them in the templates.

Plugins: Almost every aspect of Smarty is controlled through the use of plugins. They are generally as easy as dropping them into the plugin directory and then mentioning them in the template or using them in the application code.

Debugging: Smarty comes with a built-in debugging console so the template designer can see all of the assigned variables and the programmer can investigate template rendering speeds.

Compiling: Smarty compiles templates into PHP code behind the scenes, eliminating run-time parsing of templates.

Performance: Although the functionality of Smarty is quite extensive, it still performs extremely well. Most of Smarty's capabilities lie in plugins that are loaded on-demand. Many of Smarty's features would be necessary in your application anyways, resulting in quicker development/deployment. Smarty templates get compiled to PHP internally, so they take advantage of PHP op-code accelerators.

Hope this was useful.

For more info on Smarty or to get your hands dirty with Smarty go here smarty.php.net

Monday, March 20, 2006

Python, not reptilian (anymore)

Python, a hacker's tool and nothing more till a couple of years ago has suddenly burst into the limelight. I know hardcore Python fans are gonna flame me for calling Python this, but the language has matured and is usable by normal programmers only in the last couple of years. My definition of a normal programmer is someone who programs (writes code, designs stuff, graffiti and the works) day-to-day but is not addicted to it so much so that sleeping and bathing become a tiresome ritual. Let me leave out the hacker demi-gods who are happy with even LISP from the discussion on language usability for time being. They could take LISP and Perl, bang their heads and make it work, so I guess language usability/elegance might not matter much to them.

So what is so good about Python that makes it usable to the programmer community in general? I guess I would seek to answer that during the course of this post.


1. Python promotes adherence to coding standards, indentation for example. Indentation, IMHO should not be treated as an add-on feature (that is optional) but is something absolutely necessary to produce readable code. Python enforces this by making sure that blocks of code should follow the same level of indentation. For e.g. a "for" loop will work only if all the statements in it have the same level of indentation, though you are allowed to throw in braces for that good 'ol feeling. Braces don't make a difference here to the Python interpreter, the indentation surely does. (Braces signify a dictionary in python, a datatype akin to a hash table in other programming languages)

2. Python allows/encourages easy plugging in of modules. This might not be something unique to Python, but it's still wonderfully implemented. Python modules to get the most common programming tasks done are readily available and are a few key strokes away from your pet project. Python might not boast of something equivalent to CPAN (for PERL), but it’s just that re-usable Python code is all around the place and not centralized like CPAN.

3. Python is cross platform across OSes (so is PERL and C++ and Java). I believe its the best cross platform implementation (albeit with heavy favoritism for Unix like systems). It doesn't need a third party Virtual Machine (VM) to run it (it however needs python to run), nor does it rely heavily on system libraries to make it powerful. PERL is good, but I haven't seen too many PERL programmers on Windows, not yet.

4. Python is interpreted. This might be construed as a disadvantage by many because compilers are supposed to be faster and can dole out better assembly. But Python breaks that notion and is very fast for an interpreted language. Don't believe me? Try it out, it’s only a bit slower than comparable C code and C is the very definition of speed amongst high level languages. -- Python source code is automatically compiled into Python byte code by the CPython interpreter. Compiled code is usually stored in PYC (or PYO) files, and is regenerated when the source is updated, or when otherwise necessary. This bytecode is executed by the interpreter at runtime. There are options to compile a binary instead of bytecode using tools like py2exe.

5. Python is actually compiled into an intermediate form called bytecode which can then be run in a traditional interpreter. The advantage of this bytecode is its platform independence. Bytecode generated on Windows would run without any (major) modifications on UNIX flavors. However, it is to be noted that bytecode is not portable across versions of Python, so bytecode generated by Python ver 2.2 might not be compatible with Python ver 2.5.

6. Python is excellent when you want to try out some small piece of code (for say a proof of concept or an experiment). You don't need to worry about traditional boiler plate stuff here, most of that is taken care already. So you can start hacking immediately and almost all the code that you write would be actual, executable code and very little fluff.

7. To extend a little bit on point 6 (because it’s very important), Python is the best language for prototyping. When you want to hand out a prototype/model to a client for review, Python allows you to do just that, no frills attached. Its famous Rapid Application Development (RAD) paradigm is not restricted to words and you can see the difference when you start coding. I cannot emphasize enough on this point, because it could save man hours of work. A prototype which I develop over a week (without much effort) is easily throwable than something developed by a dozen guys in a month. So I would not stick with code which is not usable just because I took time to write it. What if the client changes the entire requirement/design after the prototype is ready (they are famous for it, aren't they). If I have a cute little Python prototype which does everything that the 'real' thing needs to do and it was developed in a short span of time, I can use it or throw it and not much is lost (except a few broken Python fans' hearts maybe).

8. Python is well integrated with some of the most famous libraries/toolkits out there. Python can integrate with COM, .NET, and CORBA objects. For Java libraries, use Jython, an implementation of Python for the Java Virtual Machine. For .NET, try IronPython, Microsoft's new implementation of Python for .NET. X, Tk, Motif and a gamut of other graphical toolkits are also readily available for use within your Python project, so GUI development is a snap, like never before.

9. Python is extensible. If any portion of your Python module needs speeding up, you have the option of writing it in C/C++ and integrating it with the rest of the stuff using Swig or Boost Python (there might be more, I could think of only Swig and Boost now). A brief note here, Python is good enough or quick enough for most requirements and embedding C code might really not be necessary, but you have the option nevertheless.

10. Python looks good. Isn't that silly, you would say. Why should a programming language 'look' good. I guess code needs to look good and promote readability/reviewability even if its months since you wrote it and not obscure to the point of losing some hair to understand what you wrote a couple of months ago. Most programmers spend more than half of their time reading code written by others. So its of utmost importance that code written by everyone is readable. PERL is notoriously famed for its obscure syntax which allows nearly un-decipherable code (at least for humans) to be written. If your code looks ugly in Python, it most probably is not going to work either. How do you find out if your code is ugly? I guess its more of gut instinct to start with and becomes an art with experience. So is it impossible to write code in Python that looks ugly, lacks clarity and yet works? Well, I guess someone can actually try it out as an exercise but be rest assured that Python will make it at least a dozen times tougher than PERL to write obscure code.

11. Python promotes concise code and takes obscure code out of the picture by providing easy and beautiful ways to write working code for pretty much everything. You don't need to write obscure code just because your application is really complex, you would still have an elegant way out in Python and that elegant way would be easy and obvious for most programmers. C is elegant alright, but the most elegant solutions in C might not be the most obvious.

What else in Python deserves mention, well its a long list. Visit some links I have given below and they should help you get a better idea and even a head-start with the language, if you are not already initiated.

Quotes about Python: http://www.python.org/about/quotes/

Python success stories: http://www.python.org/about/success/

Getting started: http://www.python.org/about/gettingstarted/

Do a Google on Python and you should get a wealth of information to aid you in your quest.

Welcome to the world of Python and Happy Hacking!