Home  >  Article  >  Backend Development  >  Smarty+QUICKFORM small demonstration_PHP tutorial

Smarty+QUICKFORM small demonstration_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:57:08960browse

Since the company needs quickform combined with the SMARTY development model, I have been working on it in the past few days. Let me share my experience with you. Quickform is a PEAR class library that can quickly generate JS code for form controls and verification forms. You may think this is written by hand. Isn’t it fast to generate JS and HTML? Isn’t it more troublesome to use that one? Indeed, a small number of form controls cannot show the advantages of quickform, but if there are a large number of form controls, For example, in the backend of OA, the advantages of quickform are shown. Using quickform has the characteristics of clear code and easy maintenance. It is very suitable for the development of large and medium-sized projects. What is more convenient is that it can be easily used in smarty. ^_^ Stop talking nonsense. , let’s take a look at the code, but it’s best for everyone to understand the installation of PEAR before, refer to: http://hi.baidu.com/wanghaozi/blog/item/81cfb7003f973687e850cd3e.html.
Since the quickform used by the company has been improved by itself, the code will be slightly different from what you see online. It is inconvenient to explain the copyright involved here. I will briefly show the core code so that everyone can understand it. Interested friends can read this HAOHAPPY article: http://www.phpe.net/articles/418.shtml
[php]

/*
*Author: Hui Boss
*Page: path.cfg.php
*Function: System path setting
*Copyright: Whatever copy^_^
*/

$global['path']['conf'] = $global['path']['root'] . 'conf/';//Define the system Configuration file path
$global['path']['lib'] = $global['path']['root'] . 'lib/';//Define system library file path

?>
[/php]
[php]

/*
*Author: Hui Boss
*Page: smarty.cfg.php
*Function: smarty basic configuration
*Copyright: copy as you like^_^
*/

//Define template path
$global['smarty']['template_dir '] = $global['path']['root'] . 'lib/smarty/templates';
//Define template compilation directory
$global['smarty']['compile_dir'] = $ global['path']['root'] . 'lib/smarty/templates_c';
//Define smarty configuration folder path
$global['smarty']['config_dir'] = $global[ 'path']['conf']. 'lib/smarty/configs'; smarty/cache';

//$global['smarty']['compile_check'] = true;
//Set smarty error reporting to disable
$global['smarty']['debugging '] = false;
//Turn off caching
$global['smarty']['caching'] = false;
//$global['smarty']['cache_lifetime'] = 6000;

//Define left and right boundary characters
$global['smarty']['left_delimiter'] = '<{';
$global['smarty']['right_delimiter'] = '}>';

?>
[/php]
[php]

/*
*Author: Hui Boss
*Page: common.cfg.php
*Function: Global configuration
*Copyright: copy as you like^_^
*/

$global['path'] ['root'] = dirname(__FILE__) . '/';//Set the root directory
require($global['path']['conf'] . 'conf/path.cfg.php');

require($global['path']['conf'] . 'smarty.cfg.php');
//Contains smarty class library
require($global['path'] ['lib'] . 'smarty/libs/Smarty.class.php');

//smarty configuration
$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/');//Load the pear library file
?>
[/php]
[php]

/*
*Author: Hui Boss
*Page: index.php
*Function :UI
*Copyright: copy as you like^_^
*/

require_once('common.inc.php');//Load global configuration

// Contains the quickform class library
require($global['path']['lib'] . 'pear/HTML/QuickForm.php');

$form = new HTML_QuickForm('changepwdform'); //Generate quickform instance, the parameters are form name

/*
*Start adding form elements
*The parameters are: form element type, name, (button label text), style
*/
$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','Change password','style="width:100px"');

//Add verification rules and automatically generate JS
$form->addRule('adminPwd','Password cannot be empty! ','required','','client');
$form->addRule('newPwd','The new password cannot be empty!','required','','client');
$form->addRule('newPwd2','Please enter the new password again!','required','client');
$form->addRule(array('newPwd','newPwd2' ),"The passwords entered twice are inconsistent!",'compare','','client');
$form->;//Disable form submission

//Assign form data to an array
$tpl-> assign('form_data',$form->toArray());

//Display template
$tpl->display('index.tpl');


?>
[/php]
Template code:

Copy code The code is as follows:



quickform+smarty
<{if $form_data.javascrīpt}>
  <{$form_data.javascrīpt} >
<{/if}>






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












                                                              colspan="2">

<{$form_data.btnSubmit.html}>

< /tr>
Change administrator password
Existing administrator password
< ;/td>
<{$form_data.adminPwd.html}>
New password
<{$form_data.newPwd.html}> ;
Enter the new password again



< ;/BODY>



You may wonder here why the path definition is so complicated and absolute paths are used? This is recently adapted to the needs of company projects, haha! In fact, this is helpful for deploying large projects. I believe that novices who have never been exposed to quickform or smarty will be confused by this post. Of course, I will only briefly introduce it here. I hope that if you are interested, you can continue to study it in depth and finally see the effect:
Just look at this sentence to determine whether the password entered twice is the same:
[php]
$form->addRule(array('newPwd','newPwd2'),"The passwords entered twice are inconsistent!",' compare','','client');
[/php]
Does the code look concise and clear? Haha, it will also be applied to applications combined with XAJAX. I will continue to share it with you. Learning experience, hehe!



http://www.bkjia.com/PHPjc/317876.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317876.htmlTechArticleSince the company needs quickform combined with SMARTY’s development model, I have been working on it in the past few days. Let me share my experience with you. , quickform is a PEAR class library that can quickly generate form controls and verification...
Statement:
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