RE: [SMARTY] Templates in DB

From: Date: Sun, 02 Jun 2002 18:24:56 +0000
Subject: RE: [SMARTY] Templates in DB
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4501@lists.php.net to get a copy of this message
I got it to work eventually, using PEAR DB, with some butchering, but feeling proud nonetheless. Here's what I did... index.php ---------------------------------- <?php require 'Smarty.class.php'; $smarty = new Smarty; $smarty->compile_check = true; require_once SMARTY_DIR . 'plugins/resource.db.php'; $smarty->register_resource("db", array("smarty_resource_db_source", "smarty_resource_db_timestamp", "smarty_resource_db_secure", "smarty_resource_db_trusted")); $smarty->display("db:index.tpl"); ?> ---------------------------------- resource.db.php ---------------------------------- <?php /* * Smarty plugin * ------------------------------------------------------------- * File: resource.db.php * Type: resource * Name: db * Purpose: Fetches templates from a database * ------------------------------------------------------------- */ function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty) { require_once ( 'DB.php' ); $dbhost = 'localhost'; $dbuser = 'user'; $dbpass = 'pass'; $dbname = 'Web'; $dsn = "mysql://$dbuser:$dbpass@$dbhost/$dbname"; $db = DB::connect( $dsn ); $sql = "select Temp_Src from tbl_templates where Temp_NiceID='$tpl_name'"; $db->setFetchMode(DB_FETCHMODE_ASSOC); $result = $db->query( $sql ); while ($row = $result->fetchRow()) { $tpl_source = $row['Temp_Src']; return true; } } function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty) { require_once ( 'DB.php' ); $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'user'; $dbname = 'pass'; $dsn = "mysql://$dbuser:$dbpass@$dbhost/$dbname"; $db = DB::connect( $dsn ); $sql = "select Temp_Timestamp from tbl_templates where Temp_NiceID='$tpl_name'"; $db->setFetchMode(DB_FETCHMODE_ASSOC); $result = $db->query( $sql ); while ($row = $result->fetchRow()) { $tpl_timestamp = $row['Temp_Timestamp']; return true; } } function smarty_resource_db_secure($tpl_name, &$smarty) { // assume all templates are secure return true; } function smarty_resource_db_trusted($tpl_name, &$smarty) { // not used for templates } ?> ---------------------------------- index.tpl ( Temp_NiceID ) - field in DB ---------------------------------- {include file="db:header.tpl" title=testing} {include file="db:navbar.tpl"} ---------------------------------- So I can use it to include sub templates!! If anyone can simplify this or improve it, I'd love to see it...not sure if this is the most efficient way of doing it. Best regards Simon H > -----Original Message----- > From: Simon H [mailto:simon.hamilton@ntlworld.com] > Sent: Sunday, June 02, 2002 2:03 PM > To: Jason Sweat; Smarty-General > Subject: RE: [SMARTY] Templates in DB > Importance: High > > > > For basics on how to use Smarty & PEAR DB, take a look at > > http://www.zend.com/zend/tut/tutsweatpart1.php and > > http://www.zend.com/zend/tut/tutsweatpart2.php. This > > series of tutorials is > > on just that subject. > > Thanks, this will help with the DB based content! > > > These tutorials deal with regular template, but there is a way to override > > the default template resource and retrieve them from a database. You might > > try searching this list archive, I think this topic was discussed around > > Nov-Dec of last year. > > I had a look at this, but much prefer the method described in: > > http://smarty.php.net/manual/en/plugins.resources.html > > and > > http://smarty.php.net/manual/en/templates.from.elsewhere.html > > ...since it will be easier to upgrade when required. > > I just need to know where to put the DB connectivity/config stuff, and whether > PEAR DB is required for the plugin method described in the docs...I > like PEAR DB > and have used it before in basic apps, so have no problem using it, but my PHP > is still reasonably basic. I'm still trying to come to terms with the Smarty > architecture. > > I have no previous programming experience other than HTML (not really > programming...I know) so its all new to me. > > Thanks again Jason! > > Simon H > > > > > Hope that helps. > > > > Jason > > > > > > Hi All!! > > > > > > I'm very new to Smarty, and am trying to retrieve both site content/block > > > contents and templates from a DB. > > > > > > Starting with the template; I found info on this in the documentation at: > > > > > > > > >http://smarty.php.net/manual/en/plugins.resources.html > > > > > > So I created the plugin accordingly. I haven't ran this yet, as I can't > > see how > > > it knows what database to connect to, or where the database settings are. > > > > > > Is the method shown, for use with PEAR DB? If so, where do I put the PEAR > > > includes - within the Plugin? Regardless some further information > > describing > > > how to setup DB connectivity in Smarty for Template/content storage would > > be > > > most helpful, and much appreciated. > > > > > > TIA > > > > > > Simon H > > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.smarty.general (#4501) next »