Smarty派生クラスを作成して、更に今更のpearのpear.DBを利用したSmartyの例の覚書。
phpにpearはincludeされているものとする。
下記は、Smartyバージョン2での例。
htdocs + my_app
├ Smarty/libs ← ダウンロードしたsmartyのlibs
├ templates ← 空のフォルダを作成
├ templates_c ← 空のフォルダを作成
└ config/my_app.conf
下記はmy_app.confの内容は新旧共通。
app_title="my_app_config"
db_string="mysqli://root:*******@localhost/sample_db"
db_string2="host=127.0.0.1 dbname=sample_db user=root password=*******"
root_url="https://www.example.com/"
root_dir="/var/www/html/example"
DSN = "mysql:dbname=sample_db; host=localhost; charset=utf8"
USER = "root"
PASS = "*******"
MySmarty.class.php
<?php
require_once("DB.php");
include("Smarty/libs/Smarty.class.php");
class MySmarty extends Smarty {
private $_db;
public function __construct() {
$this->Smarty();
$this->template_dir="./templates";
$this->compile_dir="./templates_c";
$this->config_dir="./config";
$this->config_load("my_app.conf",basename($_SERVER['SCRIPT_NAME'],".php"));
$this->caching=FALSE;
$this->security=TRUE;
$this->secure_dir=array("img");
$this->_db=DB::connect($this->get_config_vars("db_string"));
}
public function __destruct() {
$this->_db->disconnect();
}
public function getDb() {return $this->_db;}
}
?>
Smartyバージョン2のsample.php
<?php
ini_set('display_errors', "On");
require_once("./MySmarty.class.php");
$o_smarty=new MySmarty();
$db=$o_smarty->getDb();
$db->query('SET NAMES utf8');
$rs=$db->query("select * from uer");
//print_r($rs);
if($rs->numRows()>0){
while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)){
$data[]=array("id"=>$row['user_id'],
"name"=>$row['user_name'],
"kubun"=>$row['user_ku']);
}
}
$o_smarty->assign("data",$data);
$o_smarty->assign('title','Smarty2 User List');
$o_smarty->display("sample.tpl");
?>
Smarty3,4の場合の例。
MySmarty3.class.php
<?php
require_once("DB.php");
require_once('./Smarty3/libs/Smarty.class.php');
class MySmarty {
private $pdo;
private $_db;
/**
* コンストラクタ
* @param $smarty smartyオブジェクト
*/
public function __construct($smarty){
try {
//configs/sample.confより設定取得
$smarty->configLoad('./config/wordpress_app.conf');
$dsn = $smarty->getConfigVars('DSN');
$user = $smarty->getConfigVars('USER');
$pass = $smarty->getConfigVars('PASS');
//PDOインスタンス生成
$this->pdo = new PDO($dsn, $user, $pass);
//接続語にオプション指定
//(エラー発生時に例外をスローする指定)
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_db=DB::connect($smarty->getConfigVars("db_string"));
}
catch(PDOException $Exception) {
self::abort('エラー:' . $Exception->getMessage());
}
}
public function getDb() {return $this->_db;}
}
sample.php
<?php
ini_set('display_errors', "On");
require_once ("MySmarty3.class.php");
$smarty = new Smarty();
//v5以上の場合 $smarty = new \Smarty\Smarty;
$o_smarty = new MySmarty($smarty);
$db=$o_smarty->getDb();
$db->query('SET NAMES utf8');
$rs=$db->query("select * from user");
if($rs->numRows()>0){
while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)){
$data[]=array("id"=>$row['user_id'],
"name"=>$row['user_name'],
"kubun"=>$row['user_ku']);
}
}
$smarty->assign("data",$data);
$smarty->display("sample.tpl");
// //データベース接続終了
$db->close();
$smarty->display('sample3.tpl');
?>
sample.tpl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{$title}</title>
</head>
<body>
<h4>user list</h4>
<table border=1 >
<th>user_id</th>
<th>user_name</th>
<th>user_kubun</th>
{foreach from=$data item=datitm}
<TR>
<TD>{$datitm.id}</TD>
<TD>{$datitm.name}</TD>
<TD>{$datitm.ku}</TD>
</TR>
{/foreach}
</table>
</body>
</html>
Smartyのバージョンアップと変更点のメモです。