ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  The Dynamic Duo of PEAR::DB and Smarty
Subject:   Article shows poor understanding of Smarty & pear
Date:   2003-04-23 15:13:42
From:   anonymous2
There are many code examples in this article which are done the wrong way when working with smarty. Here's some of them and the correct code:


1. subclass Template_API is totally useless. The functions defined in this subclass are dupliacats of existing smarty functions. Instead, the author should have created a smarty object and set the template, compile and config dirs in code rather than in the constructor.


1a. setTemplate: this function is not necessay. This seems to be a carry-over from phplib type templates where the template engine had to know something about the template before parsing it. Smarty doesn't need to know anything about the template before parsing therefore the smarty function display() is used at the end of a script.


1b. assign & bulk assign: the smarty function assign() can take an associative array of key=>value pairs and assign them. The inclusing of both of these functions is unnecessary.


2. This code:
include_once("config.inc.php");
include_once(APP_INC_PATH . "class.template.php");
should NOT be needed for every page. Instead, the config.inc.php should require_once the class.template.php so the duplicate include in every file would be unnecessary.


3. This code:
include_once("DB.php");
$dbh = DB::connect("mysql://user:passwd@server/database");
is a horrible practice. By putting your userid and password and connection info to your database in every script, you are creating a maintenance nightmare. This should be done in the config.inc.php file.


4. This code:
$news = $dbhl->getAll($stmt, DB_FETCHMODE_ASSOC);
should not include DB_FETCHMODE_ASSOC. Ideal php applications should be designed to use the same fetchmode on every page so the best practice would be to create the db object in config.inc.php and call
$dbhl->setFetchMode(DB_FETCHMODE_ASSOC);
then the call in the page would be:
$dbhl->getAll($stmt);


5. As another person pointed out in these notes, the use of foreach is prefered over sections. foreach is a newer than section....the author may not be up to speed on the latest smarty versions.


Tom Anderson
My own article on the use of pear & smarty:
http://smarty.incutio.com/?page=SmartestSmartyPractices


1 to 1 of 1
  1. No, it does not (rebutal)
    2003-04-23 19:55:28  jcpm [View]

    • No, it does not (rebutal)
      2003-04-24 09:06:48  anonymous2 [View]

      • No, it does not (rebutal)
        2005-02-23 17:15:27  mitchenall [View]

1 to 1 of 1