16 Feb 2009, 1:48pm
/dev/random
by

Comments Off

Smarty Gotcha: Spaces

One of the projects I’m working on uses Smarty as its templating engine. Frankly in a post-Rails/Merb/CakePHP/Django world Smarty is pretty painful to work with and its age and implementation restrictions become readily apparent… but that’s another post for another time.

This post is about how spaces in Smarty templates might trip you up. Witness the following code from a Smarty template (yes, this is kind’a like PHP but less functional and less readable):

<img src="{$deletion_item->default_image(false)}" />
<img src="{$deletion_item->default_image( false )}" />

One would tend to think, or at least I tend to think, that those two lines of HTML/Smarty code would be functionally equivalent. They look the same, save for the second developer’s preference for some extra whitespace.

However example one works with Smarty, example two throws the following error:

Fatal error: Smarty error: [in /Users/chris/Sites/dreambank/templates/en/delete_item.tpl line 42]:
syntax error: unrecognized tag: $deletion_item->default_image( false ) (Smarty_Compiler.class.php, line 446) in /Users/chris/Sites/dreambank/includes/smarty/Smarty.class.php on line 1095

So, there you have it. There’s not much more to this than to say: when working with Smarty be very careful with your whitespace.