I’m a PHP programmer. I’ve been using it since ’02 or ’03, and I’m very familiar with the language and “enough” of the packages to get by. Over the last few days, I’ve been taking another look at Django. I’ve looked through most of its code (it’s surprisingly small, I figured it would be bigger) and am trying to wrap my head around it to see how effectively I could actually use it. Most of the time, I’m really comparing it to how I’ve solved a particular problem with PHP in the past, or seeing what sort of Program Transformation is involved between what I’ve done and what the Django devs have done.

It’s really interesting to see a big common area (and makes me satisfied, that my design isn’t crazy), but then to see something that, for me, has worked with a single method, but that in their framework has been extracted out into their entire MiddleWare suite. Me likey.

So then I looked at the templating system. First of all, some *AWESOME* ideas:

  • Inheritence really simplifies page development, by allowing more fine-grained substitutions without requiring a bajillion tiny files
  • Syntax for variables is nice. {{ blah.foo.rah.grozbag }} is very readable. But, what else to expect with Python?
  • Borrowing Smarty’s modifiers was definitely one for the win. Yes, I know that Smarty was only reusing UNIX’s familiar pipe syntax, but it’s very obvious that the Django developers are all familiar with Smarty, and likely borrowed pipes because they’ve seen it be effective in Smarty templates.

And some things I didn’t like:

  • “It’s not a programming language (like Smarty), we only allow branching and basic loops”.
  • …with no mixing of ORs and ANDs (really! Is it that hard to handle parentheses? Throw an Exception if it’s not well-formed, everyone who’s ever had to do basic algebra should be capable of using them in boolean expressions!).
  • …and branching operators that honestly reminds me of Assembler. ifequal? ifnotequal? Why can’t we use if a = b or if a == b?
  • …and some rather arbitrary functions for something that’s “not a programming language”, like {% regroup people by gender as grouped %}. I didn’t look further, but I hope they provided a more obvious plugin {{ people|regroup:"gender" }}.
  • {% blah %} is, IMO, really hard to type. I always thought the worst part of PHP output (sans smarty) was that <?= ?> was a PITA to type. {% isn’t any better, I keep typing {$ or {& on accident. Why not {{{ }}}?

So what about Smarty would I change?

  • I’ve never used the raw-PHP execute functionality, because it always sounded like just a bad idea. If full PHP functionality is required, then it’s time to write a plugin.
  • I’ve dabbled with allowing Smarty functions as MODIFIER_FUNCS, but it should be really more basic, as $smarty->allow_as_mod('strlen') or $smarty->allow_as_func('time').
  • I want to be able to do "{$foo.getFriends()[0].name}". One thing I always hate in PHP is that you can’t use complicated syntax in the language… $foo->getBar()[0] just doesn’t work, because the parser is weak. Ok, so we can’t make the PHP parser any stronger, but we can do better than Smarty does:
    1. In chains, always use nested assignments, to get around PHP’s limitation: {$foo->getBar()[0].name} translates to <? $_ = $foo->getBar(); $_ = $_[0]; print $_->name; ?>
    2. Don’t require template developers to differentiate between a->b and a.b. The PHP type system can tell the difference between an object and an array, so use it! If necessary (for speed), allow the templating engine to re-compile the template after its first run, to eliminate the type checking (assuming that if a block of code was an array this time through, it should be an array next time through).