search
HomeBackend DevelopmentPHP TutorialSmarty+QUICKFORM small demonstration_PHP tutorial

Smarty+QUICKFORM small demonstration_PHP tutorial

Jul 21, 2016 pm 03:57 PM
smartycompanydevelopmodelDemoofcombineneed

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









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











                                                              colspan="2">




Change administrator password
Existing administrator password
< ;/td>
New password

Enter the new password again


īpt>
< ;/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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools