Templating

February 5, 2009
By SeanJA

There are many ways of doing templating (for quick website development) especially in php.

One that I have a lot of experience with is Smarty.  So far I am not impressed too much with it. Rather than saving time, it seems to take up more of my time trying to get it to do what I want it to do. It also does not save me typing, which I thought was one of the points of templating.

For example in Smarty/html:

<input name="edit" type="checkbox" value="y" 
	{if $smarty.session.user and ( $user_type eq "editor" or $user_type eq "admin" )}
		checked="checked"
	{/if} /> edit

Alternatively in php/html:

<input name="edit" type="checkbox" value="y" 
<?php 
if($_SESSION['user'] && $user_type == "editor" || $user_type == "admin"){ ?>
	checked="checked"
<?php 
} ?> 
/> edit

Or in ‘pure php’ (CodeIgniter):

<?php
$this->load('form_helper');
if($_SESSION['user'] && $user_type== 'editor' || $usre_type == "admin"){
	$checked = false;
	if($checked = $this->input->get('checked');){
		$checked = true;
	}
	echo form_checkbox('edit', 'y', $checked);
}

I personally do not find it that difficult to understand either of the php examples I do understand that it may be simpler to use Smarty for very simple layouts… but when it comes to complicated layouts with many options, it is probably easier to resort to a purely php or html + php option rather than Smarty. Sure Smarty can be good if you want to let users change the layout of the page, but then, why not make it even simpler for them and have functions that create the elemets of your page and then they just have to move those around? You can even mark it with:

 =========== edit layout below =========== 

and change it so that instead of having to echo the form_checkbox helper function, you can override it and make it echo within the function itsself, then where ever you put it, it will be echoed out. In the end, this to me seems like the simpler answer to the whole thing, no real need to escape the variable, that can be done inside the function hiding it from the user as well avoiding things like:

{$variable|escape:"html"}

Which is probably more confusing to people who don’t know programming.

You may think that I am writting this because of my dislike for Smarty and all of the aggrevation that it causes me as a programmer. That is true, but there is also the problem that apparently Smarty is so rampant that you cannot get a php job without it in your resume. Oh… and look again at that if syntax:
{if isset($something) }
?>
Wait… where are the brackets? You might ask. Well… in Smarty they have done away with those brackets to make it simpler to read. If you happen to include those brackets by accident, you get Smarty compile errors. Even more inconsistancy comes up when you are trying to use arrays (you know, to group things together):
array_example_1.php

<?php
$smarty = new Smarty;
$smarty->assign('Contacts', array(
'fax' => '555-987-9876', 'email' => 'zaphod@slartibartfast.com'
);
$smarty->display('array_example_1.tpl');
?>

array_example_1.tpl

{$Contacts.fax|escape:"html"}
{$Contacts.email|escape:"html"}

Ok, so you might think that that is how arrays are handled in Smarty… and you would be right… sort of… because this is how arrays are handled when they are not hashed arrays:
array_example_2.php

<?php
$smarty = new Smarty;
$smarty->assign('Contacts', array(
'555-987-9876', 'zaphod@slartibartfast.com'
);
$smarty->display('array_example_2.tpl');
?>

array_example_2.tpl

{$Contacts[0]|escape:"html"}
{$Contacts[1]|escape:"html"}

So you can imagine the mess that is an array with hashes and other arrays inside it when you get to the layout:

array_example_3.tpl

{$Contacts[0].email.other[2]|escape:"html"}
{$Contacts.phone_number.home[1]|escape:"html"}

Don’t even get me started on the mess that is:

{literal} {/literal}

and:

{ldelim} {rdelim}

Be nice to your coders, and don’t treat your designers like idiots:

to keep designers away from PHP and to keep it safe, we should go for smarty. But is there really any designer who knows only smarty and no PHP? Is it really possible to write efficient code in smarty without knowing PHP? Nah, not at all. And there is {php} block which allow so called template designers to run unsafe PHP code inside it. Well, you can block executing {php} block from your side, but still smarty is not a solution to think that you are “secured”. And honestly, you should’t let your template designer write template code for you if they dont know PHP. And you should learn about XSS, SQL Injection and CSRF if you really want to live secured.

The Storyteller

let them use php functions and html instead of subjecting them to the bloated syntax soup that is Smarty.

Share and Enjoy:
  • Twitter
  • DZone
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • HackerNews
  • Technorati
  • Google Bookmarks
  • RSS
  • StumbleUpon
  • Yahoo! Buzz
  • LinkedIn
  • Facebook
  • Print
  • PDF
  • email

Related posts:

  1. Smarty… again
  2. Smarty Best Practices 1
  3. A better way to use jQuery in WordPress Themes
  4. Spartan Coding
  5. An alternative to Poor Man’s MVC in PHP

4 Responses to Templating

  1. Philippe on May 14, 2009 at 6:27 am

    I can’t remember the last time I saw something like that…bookmark ftw =)

  2. SeanJA on February 8, 2009 at 2:34 am

    Smarty leaves a bad taste in my mouth…

  3. gav on February 6, 2009 at 9:25 am

    Smarty is way more confusing than PHP. To keep code away from designers, let your designers only use Photoshop or Fireworks or even Gimp, and leave the coding to the coders. That’s how you get amazing website designs – someone subjected to any kind of code, templating or otherwise, might be tempted to go with what would be ‘easier’ for the coder, rather than what would look good.

    That said, don’t you hate it when you’re handed a comp that is full of dropshadows, rounded corners and unheard-of fonts?

  4. ellisgl on February 5, 2009 at 4:17 pm

    I’ve seen one decent implementation of smarty. That’s with X-Cart and their webmaster mode. Of course still coding for smarty still is a pain in the ass.

Leave a Reply

Your email address will not be published. Required fields are marked *

*