How does SMARTY make things easier to read?

How is…

{$name}


…any easier than…

<?$name?>


…?


I think using SMARTY is a lot more complicated approach. You have to learn an entirely new language/syntax and then try to keep things straight switching back and forth between the two.

That's a valid example, but it's also the most basic possible functionality of Smarty -- outputting a straight var. I really can't sit here and describe how it's more readable... as the Smarty site says "Some things can't be explained, but only experienced." But when you have thousand-line HTML pages absolutely littered with PHP formatting code and loops and conditionals, you can really appreciate the difference templating makes, even if the majority of your logic is already separated out. Which is more readable:



// What do I belong to? How am I indented legibly among the sea of PHP
// tags?
<?php } ?>

OR...

{/if}



// Grabbed from Google; the year choices aren't even dynamic.
<select name="day"><?php
  for ($i = 1; $i <= 31; $i++) {
   echo "<option value=\"$i\">$i</option>\n";
  }
 ?></select>
 <select name="month"><?php
  for ($i = 1; $i <= 12; $i++) {
   $monthname = date(‘F‘, mktime(12, 0, 0, $i, 1,
     2005));
   echo "<option value=\"$i\">$monthname</option>";
  }
 ?></select>
 <select name="year"><?php
  for ($i = 2005; $i <= 2010; $i++) {
   echo "<option value=\"$i\">$i</option>";
  }
 ?></select>

OR...

{html_select_date time="$begDate" prefix="begDate" start_year="2001" end_year="+1"}



Now look at the examples again and imagine you're a front-end developer or a designer.

Can you create your own templating system and just do this all yourself? Of course, but then you have to keep it updated and maintained yourself, you have to teach it to your colleagues, and if you want to implement some of the nicer functionality you'll want to do the caching and security features Smarty already has. I've never been a big fan of reinventing the wheel. I'd rather spend my time actually coding the application rather than working on my templating system.

That said, there are many cases where it's perfectly appropriate to roll your own templating system. Smarty is pretty low-level for systems where the templates can be modified by users. Movable Type has its own templating. Gallery 2 uses Smarty, and I think they should have written their own. Also, while Smarty is empirically fast and stable and all that, it's still a dependency. As with any other tool, you have to decide what tradeoffs you're willing to make.

I totally understand your argument about learning another syntax on top of PHP, because I made the exact same argument when I was first introduced to Smarty. I ended up being forced to learn it for work and obviously ended up liking it quite a bit (FWIW). There are really only about 5 functions/statements that are really common, and they are based on PHP syntax, so it really only takes about 30 minutes to get started.

Just to be clear, I am NOT saying everyone should use Smarty, that it's ideal for every project or even MOST projects, or that it doesn't have any flaws. It's a good tool, it has some good applications, and a lot of people find it useful. That's all.


References:
http://smarty.php.net/whyuse.php
http://en.wikipedia.org/wiki/Not_Invented_Here

_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to