_

Smarty Plugins

 - disper
 - smarty plugins *
 - tirp


Valid HTML 4.0!
Valid CSS!
Viewable with ANY browser!

Smarty is a PHP templating system for building websites. It can be extended by plugins.

ForSQL plugin

Important note: this plugin goes directly against Smarty's best practices. I think there are cases when it is useful to do so, even though the its developers disagree. So beware, using this plugin may ruin your sense of right and wrong in software design. This may be a better approach.

This plugin allows one to use a SQL SELECT statement as a for-loop. One could, for example, view the contents of a database table of an inventory with this piece of Smarty template:


  <h1>Items on stock</h1>
  <ul>
    {forsql select="SELECT * FROM items WHERE count > 0" item=stockitem}
      <li><em>{$stockitem.name}</em> ({$stockitem.count}x)
          {if $stockitem.price}&euro;{$stockitem.price|string_format:"%.2f"}{/if}
        </li>
    {/forsql}
  </ul>

This requires, of course, an existing database. Its details are specified as properties of the smarty object. For example:


  <?php
  require_once('smarty/Smarty.class.php');
  $smarty = new Smarty();
  $smarty->template_dir = 'templates';
  $smarty->compile_dir = 'templates_c';
  $smarty->caching = false;
  
  $smarty->forsql_host = 'localhost';
  $smarty->forsql_user = 'johndoe';
  $smarty->forsql_passwd = 's3cre1pwd';
  $smarty->forsql_db = 'c00ldb';

  $smarty->display('inventory.tpl');
  ?>

There code is database-specific and currently there is are versions for MySQL and SQLite. Download one of them and save it into your smarty plugins directory with filename block.forsql.php and you're set: