Apr 6 at 2:43pm, Chris de Vidal wrote:
> Given that scripts are compiled the first time they're ran, you'll
> never* notice the bloat and never lack performance; it's the same as
> real PHP code and can be optimized with a caching engine like Zend.

Certainly. However, the Smarty compiler is about 2200 lines of code, and
the Smarty class itself (which does load every time) is about 2000. Both
figures rounded up, and both files contain heaps of comments.  

Still, many people consider this bloat, if they're merely wanting to
search and replace a couple of values on an HTML template.

Those people are not really leveraging a template system, but if that's
all you wanted to do--ever--I can see why the additional overhead would be
undesirable. After all, a simple search and replace for a few values
doesn't require 4000 lines of code parsed (or 2000, after compilation).

> OK so you will notice it the first time and if you make any changes

Yes, and whenever your cached/compiled template has expired.

> For our next site, she'll mock up everything in Front Page how she wants
> it to look then I'll add template tags and business logic.  Whee!
> Separation of code from design, at last!

Working with others is one of the main benefits of a templating system. It 
doesn't matter then if it's FrontPage code or not, at least you, as the 
developer, don't have to muck with it. And it's easier to clean up later.

> It will be tempting to put all logic in the PHP but you might need some
> logic in the template; for example, we have a level check...

I'd go a step further and suggest using as much logic in the template as
possible. In a basic sense, it's display logic, not application logic.

> Instead, I put something like this in the template:
> {if $level > 2}{bio}{/if}

Again, take it a step further and you may just load up a $User object (of 
your design) within your PHP code. Then in the template, you could use:

{if $User->hasPermission('write')} ... {/if}

When you start thinking like this, your PHP logic gets very slim and easy 
to read and maintain. This is the goal and benefit of templating.

A common reaction when starting to work with Smarty is "you mean I need to
assign each variable into the template one-by-one?!" On a certain level
this is true. But as illustrated above, you can also pass arrays, objects
and other complex data structures into your template.

It becomes especially powerful when working on a larger web app. You
needn't assign your values into the template from each PHP page. You can
make a boilerplate include and assign common variables as references:

// boiler.php
$pt = new Smarty;
$pt->assign_by_ref('User',$User);

// mypage.php
require "boiler.php";
$User->doStuff();
$User->admin = true;
$pt->display('template.tpl');

So you can attach references right off the bat in an include, then every
time you use that include, you've already got things assigned and can
change them all you want before displaying, without additional assignment.

Going even one step further (the beauty of Smarty: always another level),
just extend the Smarty object itself. Then, instead of making all your
templates includes other templates (such as a header or a footer), you can
make your overall page be a template, and the extended Smarty object can 
be given extra functionality to control the page:

{* MyPage.tpl -- Smarty template for the entire page *}
...header...
{include file=$content}
...footer...

// mypage.php -- extended Smarty object
class MyPage extends Smarty {
   var $tmpl = 'MyPage.tpl';
   function render($x) {
      $this->assign('content',$x);
      $this->display($this->tmpl); } }

// actualphpcode.php -- called by the browser
$pt = new MyPage;
$pt->render('pagecontent.tpl');

Now, is Smarty awesome, or what? :)
--Kelly

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to