Home php教程 php手册 Smarty+QUICKFORM小小演示

Smarty+QUICKFORM小小演示

Jun 13, 2016 pm 12:33 PM
smarty company develop model Demo of combine need

由于公司需要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';
//定义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]

/*
*作者:辉老大
*页面: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]

/*
*作者:辉老大
*页面: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]
模板代码:

复制代码 代码如下:




quickform+smarty

    




 


 


 


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

  
  
  
    
    
  
  
    
    
  
  
    
    
  
  
    
  
修改管理员密码
现有管理员密码
    
新密码
    
再次输入新密码
    

      
    


īpt>



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

看判断两次输入密码是否一样就这一句:
[php]
$form->addRule(array('newPwd','newPwd2'),"两次输入的密码不一致!",'compare','','client');
[/php]
代码看起来是不是简洁清楚啊,呵呵,接下来还会应用到再结合XAJAX的应用,我会继续和大家分享学习心得,嘿嘿!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does WeChat's Do Not Disturb mode do? What does WeChat's Do Not Disturb mode do? Feb 23, 2024 pm 10:48 PM

What does WeChat Do Not Disturb mode mean? Nowadays, with the popularity of smartphones and the rapid development of mobile Internet, social media platforms have become an indispensable part of people's daily lives. WeChat is one of the most popular social media platforms in China, and almost everyone has a WeChat account. We can communicate with friends, family, and colleagues in real time through WeChat, share moments in our lives, and understand each other’s current situation. However, in this era, we are also inevitably faced with the problems of information overload and privacy leakage, especially for those who need to focus or

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

Do Not Disturb Mode Not Working in iPhone: Fix Do Not Disturb Mode Not Working in iPhone: Fix Apr 24, 2024 pm 04:50 PM

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Which company does the Hands app belong to? Which company does the Hands app belong to? Mar 13, 2024 am 11:10 AM

Hands-on is a brand new chat and dating software, so which company is the Hand-on-hand app? This software was created by Tianjin Laifu Cultural Development Co., Ltd. You can download it from Xiaomi Mall and Apple Mall. This introduction to the Hands-on app creation company can tell you the specific methods. The following is a detailed introduction, so take a look. Which company is the Qianshou app? Answer: Tianjin Laifu Cultural Development Co., Ltd. Detailed description: On the official software website https://www.qianshouapp.cn/, you can see the company name at the bottom. Software introduction: 1. It can filter according to the conditions that users like, and can find the objects they need faster. 2. It can help users search for the objects they need faster.

Which company does Blue Star Travel Yao belong to? Which company does Blue Star Travel Yao belong to? Mar 22, 2024 pm 03:41 PM

Blue Star Travel Ballad has been on the game hot list after the recent release of a promotional video. Many players are very curious about which company Blue Star Travel Ballad is made from. In fact, it is a new game from Shanghai 2D manufacturer Manjiu. The editor will explain it to you below. Here is the introduction of Blue Star Yuanluyao Game Company, come and take a look together. Which company does Blue Star Travel Yao come from? Answer: It was launched by Manjiu Network. 1. First of all, Blue Star Travel Yao is a game launched by Manju’s Big World RPG. A promotional video was released on March 20. 2. This product will get its version number in October 2023. The game's trademark and operating unit are both registered under the name of a company called. The latter was established in February 2023, and its official website shows that its headquarters is in Singapore. 3. The 11-minute promotional video released this time revealed this

Understanding VSCode: What is this tool used for? Understanding VSCode: What is this tool used for? Mar 25, 2024 pm 03:06 PM

"Understanding VSCode: What is this tool used for?" 》As a programmer, whether you are a beginner or an experienced developer, you cannot do without the use of code editing tools. Among many editing tools, Visual Studio Code (VSCode for short) is very popular among developers as an open source, lightweight, and powerful code editor. So, what exactly is VSCode used for? This article will delve into the functions and uses of VSCode and provide specific code examples to help readers

See all articles