[SMARTY] Is this a good way to setup Smarty?

Karl Timmermann <[email protected]>
Newsgroups gmane.comp.php.smarty.general
Message-ID <[email protected]>
I saw this on the net, and was wondering if this is a good way up to  
setup Smarty, DB, etc., and then each of my pages will require_once  
the attached file. Thanks!

-- 
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
set_env.php (text/php, 3.9 KB)
<?PHP
/**
 * set_env.php 
 * @author Tom Anderson <[email protected]>
 * @version 2.5
 *
 * This script sets up smarty, pear, and all other
 * global settings for a php application.
 */

// Client specific variables
error_reporting(E_ALL - E_NOTICE);

// db connect info
$dsn = 'mysql://root:root@localhost/database';

// Setup include path
$web_root = 'http://www.mysite.org/subdirifneeded/';
$app_root = '/home/mysite/projects/myapp/';
# note:  sometimes pear is installed at /usr/local/lib/php/ but I 
# recommend getting a copy that is local to your application so 
# you can more easily controll when pear code is updated.
$pear = $app_root . '/pear/'; 
//---------------------------------------
//--- end user edit

// Path to .ihtml (template) files
define('TEMPLATE_DIR', "$app_root/ihtml/");
$delim = (PHP_OS == "WIN32" || PHP_OS == "WINNT") ? ';': ':';
ini_set('include_path', ".{$delim}$pear{$delim}$app_root/include{$delim}$app_root");

// Set magic quotes based on database type.  If using either of these and using odbc, 
// you will need to set this by hand.  
// You should be able to safely comment this out if your system is setup right. 
ini_set('magic_quotes_sybase', ($dbType == 'mssql' || $dbType == 'sybase') ? '1': '0');

// Include pear database handler, auth object (remove if not used), and smarty
require_once 'DB.php';
require_once 'smarty/Smarty.class.php';

// Change error handling as necessary
// PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or PEAR_ERROR_CALLBACK
// PEAR::setErrorHandling(PEAR_ERROR_PRINT);
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'errhndl');
function errhndl ($err) {
    echo '<pre>' . $err->message;
    print_r($err);
    die();
} 

// Connect to the database and set fetch mode as necessary.  Include db extension as needed
// The next two lines can be safely commented if your system is setup right.
$extension = "php_$dbType" . (strpos(PHP_OS, "WIN") >= 0 ? ".dll" : ".so");
if (!function_exists($dbType . '_connect')) dl($extension);

// Connect to the database
$db = DB::connect($dsn);
$db->setFetchMode(DB_FETCHMODE_ASSOC);  // Other modes possible; I find assoc best.
$db->setOption('optimize', 'portability');  // This is really useful for me personally but may not be for everyone.

// Setup template object - NOTE:  in this example, 'smarty' is a symlink to 
// the smarty directory.  This allows you to upgrade Smarty without changing code.
$t = new smarty;
$t->template_dir = TEMPLATE_DIR;
// For other compile and cache directory options, see the comment by Pablo Veliz at the bottom of this article.
$t->compile_dir = $app_root . '/compile';
$t->cache_dir = $app_root . '/cache';
// Because you should never touch smarty files, store your custom smarty functions, modifiers, etc. in /include
$t->plugins_dir = array($app_root . '/include', $app_root . '/smarty/plugins');

// Change comment on these when you're done developing to improve performance
$t->force_compile = true;
//$t->caching = true;

## GLOBALS:  $db, $t
session_start();

/* BEGIN USER AUTH - totally optional */
// Add user authentication here if you want
// This example uses pear::Auth
require_once 'Auth/Auth.php';
$a = new Auth(
	'DB',
	array(
		'table' => 'users',
		'usernamecol' => 'login',
		'passwordcol' => 'password',
		'dsn' => $dsn
		),
	'login_function_name',
	true
);

// Use case insensitive login
if (isset($_POST['username'])) $_POST['username'] = strtoupper($_POST['username']);

// Check for logout
if ($_REQUEST['logout'] && $a->getAuth()) {
	$a->start();
	$a->logout();
	session_destroy();
	header("Location: $web_root");
	exit();
}

// Init the session for this user and authorize.
$a->start();
// You could use a global '$require_login = 1;' before 'require_once 'set_env.php';' 
// then check for it here so not all pages require login
if (!$a->getAuth()) {
	die();
}
/* END USER AUTH */

// Assign any global smarty values here.
$t->assign('web_root', $web_root);
?>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.