Smarty

更新日 2008-11-14 (金) 11:58:34

Smartyのインストール

ロジックを作成する作業と、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

EC CUBEの複数の店のユーザを単一管理

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");

を追加

管理画面の顧客管理

変更について

data/class/pages/admin/customer/LC_Page_Admin_Customer_Edit.php

197行ぐらいに追加

                    }
                    //-- 編集登録
                   $objDb->sfEditCustomerData($this->arrForm, $arrRegistColumn);
                    // 更新時にテーブルを同期させる(Edit By JE2ISM)
                    require_once("/home/okada/temp/tbsync.php");   <==追加
                }
            }
        }

        //---- ページ表示
        $objView->assignobj($this);
        $objView->display(MAIN_FRAME);

削除について

data/class/pages/admin/customer/LC_Page_Admin_Customer.php

211行あたりに追加

        // 顧客削除
        if ($_POST['mode'] == "delete") {
            $sql = "SELECT status,email FROM dtb_customer WHERE customer_id = ? AND del_flg = 0";
            $result_customer = $objQuery->conn->getAll($sql, array($_POST["edit_customer_id"]));

            if ($result_customer[0]["status"] == 2) {           //本会員削除
                $arrDel = array("del_flg" => 1, "update_date" => "NOW()");
                $objQuery->conn->autoExecute("dtb_customer", $arrDel, "customer_id = " . SC_Utils_Ex::sfQuoteSmart($_POST["edit_customer_id"]) );
            } elseif ($result_customer[0]["status"] == 1) {     //仮会員削除
                $sql = "DELETE FROM dtb_customer WHERE customer_id = ?";
                $objQuery->conn->query($sql, array($_POST["edit_customer_id"]));
            }

            // 削除時にテーブルを同期させる(Edit by JE2ISM)
            require_once("/home/okada/temp/tbsync.php");  <==追加


       }

以下のように管理者画面での変更、削除Smartyコンパイル後のファイルを変更してもよいが美しくないし、再コンパイルされると書き直す必要がある

Smartyのコンパイル後を変更

顧客管理で内容変更を行ったとき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^3807D3B7%%edit_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^1850FD4F%%index.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;
       }
   }

%%にするとabcのように取り消しになるので、数値参照文字&#x(文字コード);にするとよい

ユーザ登録画面をデフォルトに強制移動

# cd shop/data/Smarty/templates/default

# cp header.tpl header2.tpl

header2.tpl

<!--▼HEADER-->
<div id="header">
  <h1>
    <a href="http://www2.data-map.net/~okada/shop_top.html">  <==ここを変更
      <em><!--{$arrSiteInfo.shop_name|escape}-->/<!--{$tpl_title|escape}--></em>
    </a>
  </h1>
  <div id="information">

data/class/SC_View.php

103行あたりに追加

       // ヘッダとフッタを割り当て
       $header_tpl = USER_PATH . USER_PACKAGE_DIR . TEMPLATE_NAME . "/" . "header.tpl";
       $footer_tpl = USER_PATH . USER_PACKAGE_DIR . TEMPLATE_NAME . "/" . "footer.tpl";

       // ユーザー作成のテンプレートが無ければ, 指定テンプレートを割り当て
       if (!$this->_smarty->template_exists($header_tpl)) {
           if(ereg("/mypage/login.php", $_SERVER['REQUEST_URI'] ) || ereg("/entry/kiyaku.php", $_SERVER['REQUEST_URI'] ) || ereg("/entry/index.php",\
 $_SERVER['REQUEST_URI'] ) || ereg("/entry/complete.php", $_SERVER['REQUEST_URI']) || ereg("/mypage/index.php", $_SERVER['REQUEST_URI'])){
               $header_tpl = TEMPLATE_DIR . "header2.tpl";
           }else{
               $header_tpl = TEMPLATE_DIR . "header.tpl";
           }
       }
       if (!$this->_smarty->template_exists($footer_tpl)) {
           $footer_tpl = TEMPLATE_DIR . "footer.tpl";
       }

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS