Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
The benefits of the Smarty syntax

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7253
Location: Lincoln Nebraska, USA

PostPosted: Tue Apr 21, 2009 3:35 am    Post subject: The benefits of the Smarty syntax Reply with quote

Developers have had many questions about Smarty over the years. A common one of them being: Since PHP is a template language itself, why put another layer of code over top of it?

To put it bluntly, PHP is an ugly templating syntax. It works perfectly for the business side of the code, but for the presentation side, it lacks for a better purpose. Let's start with the simplest of comparisons:

PHP:

Code:
<?=$foo?>


Smarty:

Code:
{$foo}


There isn't a whole lot of difference here. What you may not notice is that PHP requires 5 "special characters" to display a variable: <?=?>, whereas Smarty only requires 2: {}. In such a short example, the benefit isn't so recognizable. Now let's try array access:

PHP:

Code:
<?=$foo['bar']?>


Smarty:

Code:
{$foo.bar}


PHP requires 9 special chars, and Smarty requires only 3. Let's see a simple foreach loop opener:

PHP:

Code:
<?php foreach($foo as $bar): ?>


Smarty:

Code:
{foreach $foo as $bar}


PHP requires 10 special chars, and Smarty requires only 2. Now let's try some real-world use, mixing things with HTML tags:

PHP:

Code:
<?php if(!empty($foo)): ?>
  <?php foreach($foo as $bar): ?>
     <a href="<?=$bar['zig']?>"><?=$bar['zag']?></a>
     <a href="<?=$bar['zig2']?>"><?=$bar['zag2']?></a>
     <a href="<?=$bar['zig3']?>"><?=$bar['zag3']?></a>
  <?php endforeach; ?>
<?php else: ?>
   There were no rows found.
<?php endif; ?>



Smarty:

Code:
{foreach $foo as $bar}
  <a href="{$bar.zig}">{$bar.zag}</a>
  <a href="{$bar.zig2}">{$bar.zag2}</a>
  <a href="{$bar.zig3}">{$bar.zag3}</a>
{foreachelse}
  There were no rows found.
{/foreach}



In the short example above, Smarty saved 110 characters, or 36% less noise. Now, perhaps the server doesn't allow short tags in PHP. The difference becomes much bigger:

PHP:

Code:
<?php echo $foo; ?>


Smarty:

Code:
{$foo}



Even with this very short example, you can see a 13 character savings (69% shorter) to display the same thing! Now let's see this mixed in with some HTML:

PHP:

Code:
<a href="<?php echo $bar['zig']; ?>"><?php echo $bar['zag']; ?></a>
<a href="<?php echo $bar['zig2']; ?>"><?php echo $bar['zag2']; ?></a>
<a href="<?php echo $bar['zig3']; ?>"><?php echo $bar['zag3']; ?></a>


Smarty:

Code:
<a href="{$bar.zig}">{$bar.zag}</a>
<a href="{$bar.zig2}">{$bar.zag2}</a>
<a href="{$bar.zig3}">{$bar.zag3}</a>


The Smarty syntax is much cleaner and easier to follow. You can also see another issue happening here: PHP uses a tag syntax that shares syntax with HTML tags. It mixes too well, and it gets difficult to tell them apart. That is where a short tag syntax (that is unlike HTML) becomes much easier on the eyes. For a designer, this is key to easy maintenance and quick changes. Employers will benefit from more efficient, and overall happier employees. (By the way, if you really want employees to quit, try having them maintain some XML/XSLT based templates for awhile. XSLT is perfect for computers, horrible for humans.)

Another significant purpose for Smarty syntax is template security. Let's use an analogy. You have a loaf of bread on a table. Which do you use to cut a slice: a bread knife, or a chainsaw? The bread knife will work wonderfully, easily slicing a piece off the loaf with little effort. The chainsaw WILL work, but you must use extreme caution. Be careful not to shred the entire loaf, or even cutting into the table top and maybe your own leg. The point here is that the Smarty syntax restricts the template logic to what it is supposed to do, and nothing more. Unleashing the full power of PHP in the presentation layer is like cutting with the chainsaw: the HTML developer could make some very dangerous mistakes (whether accidental or intentional) from within the templates. Not to mention, the templates become a syntactical mess thus harder to maintain.

You may notice the foreach syntax isn't the same as Smarty 2. This is the upcoming (easier) syntax for Smarty 3. You may also be thinking yeah, {} is not like HTML, but IS like JS/CSS, so the whole escapement problem persists. You will be glad to know that Smarty 3 will no longer require JS/CSS escapement, provided the curly braces are surrounded by white space.


Last edited by mohrt on Sat Oct 10, 2009 3:24 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 420
Location: Kathmandu, Nepal

PostPosted: Tue May 26, 2009 3:30 pm    Post subject: Missing? Reply with quote

I guess, we are missing few more things:

Say, I want to write the name of a value (say, 'Smarty course', whose id is 5, and is stored in a database.

PHP may write:
Code:
<?php echo(smarty_modifier_name(5)); ?>

While, Smarty:
Code:
{5|name}


There are many ways to make a trick. But it is true that Smarty gives a clean and well readable html. However, I do not like one thing even if we can shorten these tags:

The design view of the html does not look good with Smarty. It does not allow, particulartly, the widths of a table if the tag is too long. But, <?php and ?> tags hide even a very long names and function calls with a small icon small enough to make the design good.
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP