You use smarty plugins. This allows you to create blocks of php that can do different functions, then call those functions to the template.
Often in /smarty/libs/plugins
You normally don't have to mysql connect directly when using a plugin, but you can if you want to connect to a different db other than the local setup. As an example
Code:
function smarty_function_getpassword($params, &$smarty)
{
$user = $_SERVER[PHP_AUTH_USER];
$result = mysql_query("SELECT * FROM members WHERE username = '$user'");
$password=$row["pass"];
$smarty->assign ($params['xpassword'], $password);
return ($password);
}
Call it like: {getpassword values="xpassword"}