Extending MySQL DB resource
| From: | Tommy Ipsen | Date: | Fri, 10 Oct 2003 14:51:11 +0000 |
| Subject: | Extending MySQL DB resource | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-10328@lists.php.net to get a copy of this message | ||
Hi everybody
I'm using a db-based templatesystem and currently have the following code:
// set path to Smarty directory
define('SMARTY_DIR', '/www_test/cmjern/smarty/libs/');
// include smarty class
require_once(SMARTY_DIR . 'Smarty.class.php');
//include PEAR DB class
require_once 'DB.php';
class Smarty_CMJern extends Smarty {
function Smarty_CMJern() {
// Class Constructor. These automatically get set with each new instance.
$this->Smarty();
$this->template_dir = '/www_test/cmjern/smarty/templates/';
$this->compile_dir = '/www_test/cmjern/smarty/templates_c/';
$this->config_dir = '/www_test/cmjern/smarty/configs/';
$this->cache_dir = '/www_test/cmjern/smarty/cache/';
// Caching should be enabled in production!
$this->caching = 0; // http://smarty.php.net/manual/en/variable.caching.php
// Compile check should be disabled in production to increase performance
$this->compile_check = true; // http://smarty.php.net/manual/en/variable.compile.check.php
// Debugging should be disabled in production
$this->debugging = false; // http://smarty.php.net/manual/en/variable.debugging.php
// :WARNING: Only needed during development phase - REMOVE in production!
$this->force_compile = true;
//$this->default_resource_type = 'db';
}
}
function db_get_template($tpl_name, &$tpl_source, &$smarty_obj) {
global $db;
// do database call here to fetch your template, populating $tpl_source
$tmptpl = $db->getOne("SELECT source FROM template WHERE name='$tpl_name'");
// Always check that $tmptpl is not an error
print_r($tmptpl);
if (DB::isError($tmptpl)) {
return false;
}
elseif(empty($tmptpl)) {
return false;
}
else {
$tpl_source = $tmptpl;
return true;
}
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj) {
global $db;
// do database call here to populate $tpl_timestamp.
$tmptpl = $db->getOne("SELECT UNIX_TIMESTAMP(timestamp) FROM template WHERE name='$tpl_name'");
// Always check that $tmptpl is not an error
if (DB::isError($tmptpl)) {
return false;
}
$tpl_timestamp = $tmptpl;
return true;
}
function db_get_secure($tpl_name, &$smarty_obj) {
// assume all templates are secure
return true;
}
function db_get_trusted($tpl_name, &$smarty_obj) {
// not used for templates} // Data Source Name: This is the universal connection string $dsn = "mysql://$user:$pass@$host/$db_name"; // DB::connect will return a PEAR DB object on success // or an PEAR DB Error object on error $db = DB::connect($dsn); // With DB::isError you can differentiate between an error or // a valid connection. if (DB::isError($db)) {
die ($db->getMessage());} $db->setFetchMode('DB_FETCHMODE_ASSOC'); $smarty = new Smarty_CMJern; // register the resource name "db" $smarty->register_resource('db', array('db_get_template',
'db_get_timestamp',
'db_get_secure',
'db_get_trusted'));
//$smarty->register_resource('db', array('db_get_template', 'db_get_timestamp', 'db_get_secure', 'db_get_trusted'));
$smarty->assign('name','Just a test value');
// using resource from php script
$smarty->display('db:index.tpl');
// close conection
$db->disconnect();
?>
My table-structure for the templates are:
CREATE TABLE template (
id int(11) unsigned NOT NULL auto_increment,
name varchar(200) NOT NULL default '',
source mediumtext NOT NULL,
secure tinyint(1) NOT NULL default '1',
trusted tinyint(1) NOT NULL default '1',
timestamp timestamp(14) NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
This is my first time working with smarty and it evens seems to work - fantastic! I would however like to extend the above functionality with two features:
1. Add a pagetitle for each template to be used in the <title> tags on the html-page. I thought of adding a pagetitle field to the MySQL table but I can't figure out how I should extract the value it later on! It would be efficient to extract the value from the table while fetching the template - but that's sort of too late because I also need to assign the value to the pagetitle-variable used in the template. I hope you have some ideas or advice for me!
2. I would like to add multi-language support through the use of a "lang" variable and a corresponding language field in the db-table - does anyone has any experience or examples on doing this?
Furthermore since this is my first experience with Smarty I would be happy if any "stupid parts" in the above code could be pointed out - perhaps suggesting better alternatives in the process
Thanks in Advance,
Tommy Ipsen