Smarty + QUICKFORM little demo _ PHP Tutorial

Source: Internet
Author: User
Tags quickform
A small demonstration of Smarty + QUICKFORM. Because the company needs to use quickform in combination with the development model of SMARTY, we have made some effort in the past few days to share with you our experiences. quickform is a PEAR class library, you can quickly generate form controls and verify them. because the company needs to use quickform in combination with the SMARTY development mode, it has been a tough complement in the past few days. let's share your experience with you. quickform is a PEAR class library, you can quickly generate form controls and verify the JS code of the form. HTMLIsn't it very fast to generate? it's not more troublesome to use it. Indeed, a small number of form controls do not show the advantages of quickform, but if there are a large number of form controls, such as the background of OA, the advantages of quickform are shown. quick form has the features of clear code and easy maintenance. it is very suitable for the development of large and medium-sized projects and can be easily used in smarty, ^ _ ^ Let's talk a little nonsense. let's look at the code. However, you 'd better understand the installation of PEAR before. refer to: idea.
Because quickform used by the company has been improved by itself, there will be some differences between the code and what you see on the internet. it is inconvenient to describe the copyright here, and the core code will be briefly displayed, if you are interested FriendLet's take a look at this HAOHAPPY article: http://www.phpe.net/articles/418.shtml
[Php]

/*
* Author: Boss Hui
* Page: path. cfg. php
* Function: set the system path.
* All rights reserved: copy everything _^
*/

$ Global ['path'] ['conf'] = $ global ['path'] ['root']. 'conf/'; // defines the path of the system configuration file.
$ Global ['path'] ['Lib'] = $ global ['path'] ['root']. 'Lib/'; // defines the path of the system library file

?>
[/Php]
[Php]

/*
* Author: Boss Hui
* Page: smarty. cfg. php
* Function: basic smarty configuration
* All rights reserved: copy everything _^
*/

// Define the template path
$ Global ['smarty '] ['Template _ dir'] = $ global ['path'] ['root']. 'Lib/smarty/templates ';
// Define the template compilation Directory
$ Global ['smarty '] ['compile _ dir'] = $ global ['path'] ['root']. 'Lib/smarty/templates_c ';
// Define the path of the smarty configuration folder
$ 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;
// Set the smarty error to disable
$ Global ['smarty '] ['destgging'] = false;
// Disable Cache
$ Global ['smarty '] ['caching'] = false;
// $ Global ['smarty '] ['cache _ lifetime'] = 6000;

// Define the left and right boundary operators
$ Global ['smarty '] ['left _ delimiter'] = '<{';
$ Global ['smarty '] ['right _ delimiter'] = '}> ';

?>
[/Php]
[Php]

/*
* Author: Boss Hui
* Page: common. cfg. php
* Function: global configuration
* All rights reserved: copy everything _^
*/

$ 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 the 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 '] ['destgging'];
$ 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: Boss Hui
* Page: index. php
* Function: UI
* All rights reserved: copy everything _^
*/

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 a quickform instance. the parameter is the form name.

/*
* Start adding form elements
* The parameters are in sequence: form element type, name, (button label text), and 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 to automatically generate JS
$ Form-> addRule ('adminpwd', 'the password cannot be blank! ', 'Requestred', '', 'client ');
$ Form-> addRule ('newpwd', 'the new password cannot be blank! ', 'Requestred', '', 'client ');
$ Form-> addRule ('newpwd2 ',' Enter the new password again! ', 'Requestred', 'client ');
$ Form-> addRule (array ('newpwd', 'newpwd2 '), "the two passwords are inconsistent! ", 'Company','', 'client ');
$ Form->; // The form cannot be submitted.

// Allocate form data to the array
$ Tpl-> assign ('form _ data', $ form-> toArray ());

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


?>
[/Php]
Template code:

The code is as follows:




Quickform + smarty
<{If $ form_data.w.cr =pt}>
<{$ Form_data.w.cr =pt}>
<{/If}>











Here, we may wonder why the definition of paths is so complicated and absolute paths are used? This is recently adapted to the needs of the company's projects! In fact, this facilitates the deployment of large projects. This post is intended for beginners who have never been familiar with quickform or smarty. of course, I am just a brief introduction here. if you are interested, I hope you can continue to study it in depth, finally, let's look at the effect:

Check whether the two passwords are the same:
[Php]
$ Form-> addRule (array ('newpwd', 'newpwd2 '), "the two passwords are inconsistent! ", 'Company','', 'client ');
[/Php]
Isn't the code concise and clear? well, it will be applied in combination with XAJAX. I will continue to share my learning experience with you!

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.