ロジックを作成する作業と、HTMLデザインを編集する作業を分担して行うことが可能になるPHPのテンプレートエンジンとしてSmartyがある。
http://www.smarty.net/download.phpからダウンロード。
$ cd /usr/local/src $ tar zxvf Smarty-2.6.19.tar.gz $ cd /usr/local/src $ su # mv src/Smarty-2.6.19 . # exit $ cd
ホームディレクトリに作成するテンプレートを入れるフォルダsmatry_testを作成しディレクトリ構造を作成する(cache/ configs/ templates/ templates_c/を作成)
$ mkdir smatry_test $ cd smatry_test $ mkdir cache $ mkdir configs $ mkdir templates $ mkdir templates_c $ su # chown -R apache.apache cache # chown -R apache.apache templates_c
Smartyでwebアクセスする場所をsmarty_testとするため
# mkdir /var/www/html/smarty_test
設定ファイル
Smartyの環境を呼び出すのに必要
/var/www/html/smarty_test/init.php
<?php
// Linux スタイル (大文字の 'S' に注意)
define('SMARTY_DIR', '/usr/local/Smarty-2.6.19/libs/'); <==Smartyの展開ディレクトリ
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
// テンプレート、キャシュ等のディレクトリを指定。
$smarty->template_dir = '/home/okada/smatry_test/templates/';
$smarty->compile_dir = '/home/okada/smatry_test/templates_c/';
$smarty->config_dir = '/home/okada/smatry_test/configs/';
$smarty->cache_dir = '/home/okada/smatry_test/cache/';
?>
/var/www/html/smarty_test/test.phpにアクセスページ(testページ)を指定
<?php
require_once("/var/www/html/smarty_test/init.php");
$smarty->assign('name','JE2ISM');
$smarty->assign('name2','JF2LYU');
//** 次の行のコメントをはずすと、デバッギングコンソールを表示します
//$smarty->debugging = true;
$smarty->display('index.tpl');
$smarty->display('index2.tpl');
?>
アクセス
http://(サーバ)/smarty_test/test.php
DBからテーブルをエクスポートしてそれを別テーブルにインポート。同じユニークキーが ある時は上書きする。
tbsync.php
<?php
$cmd = "/usr/bin/mysql -B --skip-column-names -u ism --password=*** -D eccube_ism -e 'select * from dtb_customer;' -h 192.168.30.16";
exec($cmd, $output);
$fp=fopen("/home/okada/temp/dtb_customer.txt","w");
foreach ($output as $a){
fputs($fp,$a."\n");
}
fclose($fp);
$cmd ="/usr/bin/mysqlimport --local eccube_ism2 /home/okada/temp/dtb_customer.txt-u ism --password=*** -r -h 192.168.30.16";
exec($cmd);
?>
html/entry/complete.phpとhtml/mypageのrefusal_complete.phpとchange_complete.php最後の行に
require_once("/home/okada/temp/tbsync.php");
を追加
管理画面の顧客管理
顧客管理で内容変更を行ったときtbsync.phpを実行させるにはSmartyのテンプレート上に記入するとよいのでテンプレートでphp実行できるように
data/module/Smarty/libs/Smarty.class.phpを
* @var integer
*/
/* var $php_handling = SMARTY_PHP_PASSTHRU;*/
var $php_handling = SMARTY_PHP_ALLOW; <==変更
* @var array
*/
var $security_settings = array(
/* 'PHP_HANDLING' => false, */
'PHP_HANDLING' => true, <==変更
'IF_FUNCS' => array('array', 'list',
'isset', 'empty',
'count', 'sizeof',
'in_array', 'is_array',
'true', 'false', 'null'),
'INCLUDE_ANY' => false,
/* 'PHP_TAGS' => false, */
'PHP_TAGS' => true, <==変更
'MODIFIER_FUNCS' => array('count'),
'ALLOW_CONSTANTS' => false
);
のように変更する
本来はdata/Smarty/templates_c/default/admin/edit_complete.tplに記載すればよいがデリミターが<!--{ }-->とdata/module/Smarty/libs/Smarty.class.phpに定義されいるため、<!--{php}-->がコメントになってしまうようなので、コンパイル済の
data/Smarty/templates_c/default/admin/38^380^3807D3B7edit_complete.tpl.phpに
</form>
</table>
<!-- 以下を追加 -->
<?php
require_once("/home/okada/temp/tbsync.php");
?>
<!--★★メインコンテンツ★★-->
これはコンパイル澄みなためdata/Smarty/templates_c/default/admin/edit_complete.tplを変更した場合は追加しなおす必要あり。
削除について
data/Smarty/templates_c/default/admin/18^185^1850FD4Findex.tpl.php
function fnDelete(customer_id) {
if (confirm('この顧客情報を削除しても宜しいですか?')) {
document.form1.mode.value = "delete"
document.form1['edit_customer_id'].value = customer_id;
document.form1.submit();
<?php -|
require_once("/home/okada/temp/tbsync.php"); |<==追加
?> -|
return false;
}
}