Smarty+QUICKFORM小小演示


Posted in PHP onFebruary 25, 2007

由于公司需要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]
<?php

/*
*作者:辉老大
*页面:path.cfg.php
*功能:系统路径设置
*版权所有:随便copy^_^
*/

$global['path']['conf']     = $global['path']['root'] . 'conf/';//定义系统配置文件路径
$global['path']['lib']      = $global['path']['root'] . 'lib/';//定义系统库文件路径

?>
    [/php]
[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';
//定义smarty配置文件夹路径
$global['smarty']['config_dir']         = $global['path']['conf'] . 'lib/smarty/configs';
$global['smarty']['cache_dir']             = $global['path']['root'] . 'lib/smarty/cache';

//$global['smarty']['compile_check']         = true;
//设置smarty报错禁用
$global['smarty']['debugging']             = false;
//关闭缓存
$global['smarty']['caching']             = false;
//$global['smarty']['cache_lifetime']     = 6000;

//定义左右边界符
$global['smarty']['left_delimiter']     = '<{';
$global['smarty']['right_delimiter']     = '}>';

?>
[/php]
[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']['compile_dir'];
$tpl->config_dir           = $global['smarty']['config_dir'];

$tpl->debugging         = $global['smarty']['debugging'];
$tpl->caching             = $global['smarty']['caching'];
$tpl->cache_lifetime     = $global['smarty']['cache_lifetime'];

$tpl->left_delimiter     = $global['smarty']['left_delimiter'];
$tpl->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]
<?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]
模板代码:

<HTML> 
<HEAD> 
<TITLE>quickform+smarty</TITLE> 
<{if $form_data.javascrīpt}> 
    <{$form_data.javascrīpt}> 
<{/if}> 
</HEAD> <BODY> 
<p> </p> 
<p> </p> 
<p> </p> 
<form <{$form_data.attributes}> > 
<table width="300"  border="0" align="center" cellpadding="3" cellspacing="3" 
bgcolor="#F6F6F6" style="font-size:9pt" class="AddTable"> 
<tr bgcolor="#FFFFFF"> 
  <td width="47%" colspan="2"><div align="center">修改管理员密码</div></tr> 
  <tr> 
  <tr> 
    <td width="47%"><div align="center">现有管理员密码 
    </div></td> 
    <td width="53%"><{$form_data.adminPwd.html}></td> 
  </tr> 
  <tr> 
    <td><div align="center">新密码 
    </div></td> 
    <td><{$form_data.newPwd.html}></td> 
  </tr> 
  <tr> 
    <td><div align="center">再次输入新密码 
    </div></td> 
    <td><{$form_data.newPwd2.html}></td> 
  </tr> 
  <tr> 
    <td colspan="2"><div align="center"> 
      <{$form_data.btnSubmit.html}> 
    </div></td> 
  </tr> 
</table> 
</form> 
<scrīpt type="text/javascrīpt" src="response.js"></scrīpt> 
</BODY> 
</HTML>

这里大家也许觉得奇怪,为什么路径要定义这么复杂,而且使用绝对路径呢?这个是最近适应公司项目的需要,呵呵!其实这样有利于部署大的项目。这个帖子相信没接触过quickform或smarty的新手一定看的一头雾水,当然,我在这也只是简单介绍下,希望大家有兴趣的可以继续深入研究,最后看看效果:

看判断两次输入密码是否一样就这一句:
[php]
$form->addRule(array('newPwd','newPwd2'),"两次输入的密码不一致!",'compare','','client');
[/php]
代码看起来是不是简洁清楚啊,呵呵,接下来还会应用到再结合XAJAX的应用,我会继续和大家分享学习心得,嘿嘿!

PHP 相关文章推荐
php模拟socket一次连接,多次发送数据的实现代码
Jul 26 PHP
解析php中heredoc的使用方法
Jun 17 PHP
解析php框架codeigniter中如何使用框架的session
Jun 24 PHP
YII模块实现绑定二级域名的方法
Jul 09 PHP
yii实现创建验证码实例解析
Jul 31 PHP
PHP rsa加密解密使用方法
Apr 27 PHP
PHP Cookei记录用户历史浏览信息的代码
Feb 03 PHP
如何使用PHP给图片加水印
Oct 12 PHP
ThinkPHP实现生成和校验验证码功能
Apr 28 PHP
PHP排序算法之快速排序(Quick Sort)及其优化算法详解
Apr 21 PHP
PHP实现微信申请退款功能
Oct 01 PHP
PHP获取真实IP及IP模拟方法解析
Nov 24 PHP
php简单封装了一些常用JS操作
Feb 25 #PHP
实现了一个PHP5的getter/setter基类的代码
Feb 25 #PHP
php公用函数列表[正则]
Feb 22 #PHP
发布一个用PHP fsockopen写的HTTP下载的类
Feb 22 #PHP
隐藏X-Space个人空间下方版权方法隐藏X-Space个人空间标题隐藏X-Space个人空间管理版权方法
Feb 22 #PHP
excellent!――ASCII Art(由目标图象生成ascii)
Feb 20 #PHP
珊瑚虫IP库浅析
Feb 15 #PHP
You might like
php生成局部唯一识别码LUID的代码
2012/10/06 PHP
php实现json编码的方法
2015/07/30 PHP
PHP封装的验证码工具类定义与用法示例
2018/08/22 PHP
laravel返回统一格式错误码问题
2019/11/04 PHP
PHP设计模式之命令模式示例详解
2020/12/20 PHP
XP折叠菜单&amp;仿QQ2006菜单
2006/12/16 Javascript
两个Javascript小tip资料
2010/11/23 Javascript
js获取网页可见区域、正文以及屏幕分辨率的高度
2014/05/15 Javascript
Javascript实现简单二级下拉菜单实例
2014/06/15 Javascript
jquery实现简单的自动播放幻灯片效果
2015/06/13 Javascript
javascript实现跨域的方法汇总
2015/06/25 Javascript
深入浅析JavaScript中的Function类型
2016/07/09 Javascript
JavaScript关于提高网站性能的几点建议(一)
2016/07/24 Javascript
JavaScript奇技淫巧44招【实用】
2016/12/11 Javascript
js实现图片轮播效果学习笔记
2017/07/26 Javascript
基于vue 添加axios组件,解决post传参数为null的问题
2018/03/05 Javascript
理解 JavaScript EventEmitter
2018/03/29 Javascript
js实现文件上传功能 后台使用MultipartFile
2018/09/08 Javascript
json字符串对象转换代码实例
2019/09/28 Javascript
React生命周期原理与用法踩坑笔记
2020/04/28 Javascript
vue axios请求成功却进入catch的原因分析
2020/09/08 Javascript
[03:20]次级联赛厮杀超职业 现超级兵对拆世纪大战
2014/10/30 DOTA
学习python类方法与对象方法
2016/03/15 Python
pycharm修改界面主题颜色的方法
2019/01/17 Python
Django csrf 两种方法设置form的实例
2019/02/03 Python
Django中使用session保持用户登陆连接的例子
2019/08/06 Python
python mqtt 客户端的实现代码实例
2019/09/25 Python
Tensorflow 实现将图像与标签数据转化为tfRecord文件
2020/02/17 Python
澳大利亚设计师服装在线:MISHA
2019/10/07 全球购物
小米官方旗舰店:Xiaomi
2020/08/07 全球购物
大学生入党思想汇报
2014/01/14 职场文书
会计学生自我鉴定
2014/02/06 职场文书
数学高效课堂实施方案
2014/03/29 职场文书
百年校庆宣传标语口号
2015/12/26 职场文书
低版本Druid连接池+MySQL驱动8.0导致线程阻塞、性能受限
2021/07/01 MySQL
MySQL之MyISAM存储引擎的非聚簇索引详解
2022/03/03 MySQL