OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting Dreamweaver Templates

Search This Site :     PHP Function Reference :
 

PHP Template Engine Comparison

By admin (2007-11-02. 1823 views.)
This article gives an overview comparison of the Smarty, Savant and Pear Template It engines.

Smarty Templates

Smarty templates are primarily HTML pages that have special keywords loaded that the Smarty processor evaluates into a cache at run time.

Here is a sample PHP and template directly from http://smarty.php.net

<?php

include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name''george smith');
$smarty->assign('address''45th & Harris');

// display it
$smarty->display('index.tpl');

?>


Smarty Template

<?php

<html>
<
head>
<
title>User Info</title>
</
head>
<
body>

User Information:<p>

Name: {$name}

Address: {$address}


</
body>
</
html>

?>


Discussion of Smarty

The $smarty object is really a wrapper to a whole set of underlying Smarty libraries available to you. assign() places named values into an array. display() takes the template mentioned, substitutes values for fields that mactch previous assignments and generates the corresponding HTML page to the browser.


Savant Templates

Savant templates are primarily HTML pages that standard PHP tags embedded. The PHP code in the template references magic variable names per the Savant naming convention.

Here is a sample PHP and template directly from http://phpsavant.com

<?php

// Load the Savant2 class file and create an instance.
require_once 'Savant2.php';
$tpl =& new Savant2();

// Create a title.
$name "Some Of My Favorite Books";

// Generate an array of book authors and titles.
$booklist = array(
    array(
        
'author' => 'Hernando de Soto',
        
'title' => 'The Mystery of Capitalism'
    
),
    array(
        
'author' => 'Neal Stephenson',
        
'title' => 'Cryptonomicon'
    
),
    array(
        
'author' => 'Milton Friedman',
        
'title' => 'Free to Choose'
    
)
);

// Assign values to the Savant instance.
$tpl->assign('title'$name);
$tpl->assign('books'$booklist);

// Display a template using the assigned values.
$tpl->display('books.tpl.php');

?>


Savant Template

<?php

<html>
    <
head>
        <
title><?php $this->_($this->title?></title>
    </head>

    <body>
        
        <?php if (is_array($this->books)): ?>
            
            <!-- A table of some books. -->
            <table>
                <tr>
                    <th>Author</th>
                    <th>Title</th>
                </tr>
                
                <?php foreach ($this->books as $key => $val): ?>
                    <tr>
                        <td><?php echo $this->_($val['author']) ?></td>
                        <td><?php echo $this->_($val['title']) ?></td>
                    </tr>
                <?php endforeach; ?>
                
            </table>
            
        <?php else: ?>
            
            <p>There are no books to display.</p>
            
        <?php endif; ?>
        
    </body>
</html>

?>


Discussion of Savant

In the PHP code we assign name/value pairs. Notice that you really have embedded PHP code in your template. Savant gives you a standard way to pass variables into your templates.


Template It Templates

Template It templates are HTML pages that have specially named tags that Template It converts and replaces at run time with values provided by the PHP code.

Here is a sample PHP and template directly from http://pear.php.net/manual/en/package.html.html-template-it.intro.php

<?php

require_once "HTML/Template/IT.php";

  
$data = array
  (
    
"0" => array("Stig""Bakken"),
    
"1" => array("Martin""Jansen"),
    
"2" => array("Alexander""Merz")
  );

  
$tpl = new HTML_Template_IT("./templates");

  
$tpl->loadTemplatefile("main.tpl.htm"truetrue);

  foreach(
$data as $name) {
    foreach(
$name as $cell) {
        
// Assign data to the inner block
        
$tpl->setCurrentBlock("cell") ;
        
$tpl->setVariable("DATA"$cell) ;
        
$tpl->parseCurrentBlock("cell") ;
    }

     
// parse outter block
     
$tpl->parse("row");
  }
  
// print the output
  
$tpl->show();

?>


Template It Template

<?php

<html
 <
table border
<!-- 
BEGIN row --> 
  <
tr>
<!-- 
BEGIN cell -->  
   <
td>
    {
DATA}
   </
td>
<!-- 
END cell -->  
  </
tr>
<!-- 
END row --> 
 </
table
</
html

?>


Discussion of Template It

In Template It the PHP code executes in a lock-step correspondence with the components of the HTML page being generated.


Overall

My estimate is that Smarty is the clear leader in the field for PHP templating. There is a clear delineation between the code and the template being used.

Savant appears to offer a cleaner way for PHP developers to get into templating. However, I dislike the inclusion of actual PHP code in the templates. It seems to go against the standard MVC model.

Template It does not appear to offer any advantage over the others. The template constructs are complicated. This may appeal to Pear fanatics.
 

 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting


Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS Free Templates

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005