smartprinter怎麼用 Smarty+QUICKFORM小小演示

WBOY
發布: 2016-07-29 08:36:23
原創
1019 人瀏覽過

由於公司需要quickform結合SMARTY的開發模式,最近幾天惡補了下,跟大家分享下心得吧,quickform是一個PEAR類別庫,可以快速生成表單控制項及驗證表單的JS程式碼,大家可能覺得這個用手寫JS和HTML生成不是很快嗎,用那個不是更麻煩,的確,少量的表單控件是顯示不出quickform的優勢的,但是如果有大量的表單控件,例如OA的後台,quickform的優勢就顯示出來了,利用quickform有代碼清晰,易於維護等特點,非常適合大中型項目的開發,更方便的是可以在smarty中輕鬆使用它,^_^廢話少說,來看看代碼,不過大家之前最好先了解下PEAR的安裝,請參考:http://hi.baidu.com/wanghaozi/blog/item/81cfb7003f973687e850cd3e.html。
    由於公司用的quickform是自己改進過的,因此代碼和大家網上看到的會有些差別,涉及版權在這裡就不便說明,簡要展示下核心代碼,大家就當了解下吧,有興趣的朋友可以看看這篇HAOHAPPY的文章:http://www.phpe.net/articles/418.shtml
    [php]
/*
*作者:輝老大
*頁:path.cfg.php
*功能:系統路徑設定
*版權所有:隨便copy^_^
*/
$global['path']['conf']     = $global['path']['root'] . 'conf/';//定義系統設定檔路徑
$global['path'] ['lib']      = $global['path']['root'] . 'lib/';//定義系統函式庫檔案路徑
?>
    [/php]
[php]
/*
*作者:輝老大
*頁:smarty.cfg.php
*功能:smarty基本配置
*版權所有:隨便copy^_^
*/
//定義模板路徑
$global['smarty']['template_dir']       = $global['path']['root'] . 'lib/smarty/templates';
//定義模板編譯目錄
$global['smarty']['compile_dir']        = $global['path']['root'] . 'lib/smarty/templates_c';
/root'] . 'lib/smarty/templates_c';
/ /定義smarty設定檔夾路徑
$global['smarty']['config_dir']         = $global['path']['conf'] . 'lib/smarty/configs';
$global[ 'smarty']['cache_dir']             = $global['path']['root'] . 'lib/smarty/cache';
//$global['smarty']['comp/eckm] ;
//設定smarty錯誤停用
$global['smarty']['debugging']             = false;
/////////>$global[ = false;
//$global['smarty']['cache_lifetime']     = 6000;
//定義左右邊界符號
$global['smarty']['left_delimiter']   {';
$global['smarty']['right_delimiter']     = '}>';
?>
[/php]
[php]
/*
*作者:輝老大
*頁:common.cfg.php
*功能:全域設定
*版權所有:隨便copy^_^
*/
$global['path']['root'] = dirname(__FILE__) . '/';//設定根目錄
require($global['path']['conf'] . 'conf/path. cfg.php');
require($global['path']['conf'] . 'smarty.cfg.php');
//包含smarty類別庫
require($global[ 'path']['lib']  . 'smarty/libs/Smarty.class.php');
//smarty配置
$tpl = new Smarty();
$tpl->template_dir       $global['smarty']['template_dir'];
$tpl->compile_dir          = $global['smarty'][' ]['config_dir'];
$tpl->debugging         = $global['smarty']['debugging'];
$tpl->caching   ;
$tpl->cache_lifetime     = $global['smarty']['cache_lifetime'];
$tpl->left_delimiter     = $global['smarty']['smarty']fleft_del ->right_delimiter     = $global['smarty']['right_delimiter'];
unset($global['smarty']);
ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . $global['path']['lib'] . 'pear/');//載入pear函式庫檔案
?>
[/php]
[php]
/*
*作者:輝老大
*頁:index.php
*功能:UI
*版權所有:隨便copy^_^
*/
require_once('common.inc.php');//載入全域配置
//包含quickform類別庫
require($global['path']['lib'] . 'pear/HTML/ QuickForm.php');
$form = new HTML_QuickForm('changepwdform');//產生quickform實例,參數為表單名稱
/*
*開始新增表單元素
*參數依序為:表單元素類型,名稱,(按鈕標籤文字),樣式
*/
$form->addElement('password','adminPwd','','style="width:120px"');
$form->addElement('password','newPwd','','style="width:120px"');
$form->addElement('password','newPwd2','' ,'style="width:120px"');
$form->addElement('submit','btnSubmit','修改密碼','style="width:100px"');
//增加驗證規則,自動產生JS
$form->addRule('adminPwd','密碼不能為空! ','required','','client');
$form->addRule('newPwd','新密碼不能為空!','required','','client');
$form->addRule('newPwd2','請再輸入新密碼!','required','client');
$form->addRule(array('newPwd','newPwd2'),"兩次輸入的密碼不一致!",'compare','','client');
$form->;//禁止提交表單
//分配表單資料到數組
$tpl->assign('form_data' ,$form->toArray());
//顯示範本
$tpl->display('index.tpl');
?>
[/php]
範本程式碼:

複製程式碼 程式碼如下:








    


 


 


 


 >
bgcolor="#F6F6F6" style="font-size:9pt" class="AddTable">

  

  
      width="53%">
  
  
    
    
   再輸入新密碼
    
    
  
  
    
修改管理員密碼
現有管理員密碼
    
新密碼
    

       🎜>  


īpt>




這裡大家或許覺得奇怪,為什麼路徑要定義這麼複雜,而且使用絕對路徑呢?這是最近適應公司專案的需要,呵呵!其實這樣有利於部署大的專案。這篇文章相信沒接觸過quickform或smarty的新手一定看的一頭霧水,當然,我在這也只是簡單介紹下,希望大家有興趣的可以繼續深入研究,最後看看效果:
看判斷兩次輸入密碼是否一樣就這一句:[php]
$form->addRule(array('newPwd','newPwd2'),"兩次輸入的密碼不一致!",'compare',' ','client');
[/php]
程式碼看起來是不是簡潔清楚啊,呵呵,接下來還會應用到再結合XAJAX的應用,我會繼續和大家分享學習心得,嘿嘿!

以上就介紹了smartprinter怎麼用 Smarty+QUICKFORM小小演示,包括了smartprinter怎麼用方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!