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(APP_INC_PATH . "class.template.php");
should NOT be needed for every page. Instead, the should require_once the class.template.php so the duplicate include in every file would be unnecessary.

3. This code:
$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 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 and call
then the call in the page would be:

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:

Full Threads Oldest First

Showing messages 1 through 3 of 3.

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

    1. Did you read what I said about the Template_API class ? I only create it so I don't have to set all of the appropriate variables and to give me an extra taste of security in thinking that I could use the same front-end code with different templating engines.

    2. I understand what you are trying to say, but the include is necessary because there will be cases where some pages / scripts in your web application will not need the template class. Instead of having to parse the Template_API class, and the Smarty classes on every page hit, putting it on a page by page basis helps in performance a little bit.

    3. Please, let's not over-react here. It was just supposed to be a simple example, not a best-practice type of article. It would be a very long article if I needed to describe and show all helper functions and includes that I usually use for simple examples like the one you are talking about here.

    4. Why are ideal PHP applications supposed to use the same fetchmode on every page ? I know the trick of using the setFetchMode() method but I don't want to lose the flexibility of being able to specify a different one when I need to / want to.

    5. I'm totally up to speed with the latest versions of Smarty, but why is the use of {foreach} prefered over {section} ? There is no difference in functionality. I personally prefer to use {section} but as I said to the other poster, that is just a personal opinion. Similar to the use of 'foreach' or 'for' in PHP, may I say so.

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

      Not trying to flame, just a discussion. With that said:

      1. K, I probably didn't read your article close enough when you explained the why of it. I personally don't plan on ever using anything besides smarty, hence I thought it was unnecessary. I still do think it's unnecessary, though. For this article, you're reviewing pear & smarty; for this reason I think the use of the extra class for non-smarty template engines is unnecessary...if the article had a broader scope to include other engines (I'm very glad it doesn't), I could see the justification for the class.

      2. My personal experience has shown that the number of pages that do NOT require database access in a complicated php application is nearly zero. Because the exception is rare, I prefer to set a global variable if db access will not be necessary. So, for the

      if (!$_GLOBALS['_skip_db']) require_once 'db.class.php';

      Then, all pages that need db access have:

      require_once '';

      as their first line and the rare page that does not need db access has:

      $_GLOBALS['_skip_db'] = true;
      require_once '';

      3. From your reply: "It was just supposed to be a simple example, not a best-practice type of article" but from the article "...I will show throughout this article my experiences and best practices in integrating these two amazing libraries."
      This is the reason I pointed out the db connection info in each script...the article did mention best practices.

      4. IMHO, ideal php applications should use the same fetchmode throughout so maintenance is easier. If you can work on any page in an application and know that it's using an ASSOC fetch mode, you will save a lot of time. Also, if an application does not use the same fetch mode globally than you will need to spend extra time on every edit to check the fetch mode. Personally, I try to code all my applications in such a way that the next person to edit them won't hate me. I think application-wide standards for things like fetch modes is a good way to do that.

      5. The syntax for section is not as clean as foreach. It does has some features which foreach does not, but, for basic use, foreach will save you a lot of typing (e.g. foreach does not require you to name the iterator when specifying a specific value in the current loop).

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

        5. I agree... the section tag is the wrong thing to use in this instance. It's confusing to both designers and future template maintainers and doesn't serve any good purpose in the example given as far as I can tell.