Use Smarty to separate forms and functions in PHP applications-PHP source code

Source: Internet
Author: User
Tags php template
Ec (2); mixing PHP with other Web page tags will lead to confusion in program logic, HTML, Cascading Style Sheets (CascadingStyleSheets, CSS), and JavaScript, make maintenance a daunting task. The Smarty template engine can separate forms from functions. Php web applications are easy to use. The PHP syntax is neat and easy to understand. PHP, HTML, and JavaScrip. script ec (2); script

Mixing PHP with other Web page tags at will make the program logic, HTML, Cascading Style Sheet (CSS), and JavaScript messy, making maintenance a daunting task. The Smarty template engine can separate forms from functions.

PHP Web applications are easy to use. The PHP syntax is neat and easy to understand. PHP can be directly mixed with HTML, JavaScript, and CSS to quickly generate visual results. In addition, deploying PHP applications to your own Web servers or hosting services is just a piece of cake.

However, mixing PHP with other page tags is also a responsibility. PHP code is usually Structured Query Language (SQL) complex web (not joke) for queries, functions, classes, developer comments, HTML, CSS styles, and scripts ). What's worse, the content is stored in PHP,echoThere are many methods to send data to the output buffer. It is very laborious to maintain such messy pages. Making insignificant changes to code or tags can cause serious damage, and page enhancement may require the joint efforts of designers and programmers. Use PHP,Form(Page layout) andFunction(The purpose and structure of the page) will be mixed together.

Ideally, forms and functions are independent of each other. For example, CSS and HTML must be the same. CSS is a form, while HTML is a function. When using PHP, it is ideal if the page tag and code can be separated. The Code processes the input, makes a decision, and generates the displayed data, and the tag will expect the data to be obtained and provide the desired bracket for rendering information.

For example, the homepage tag may leave a "fill in the blank" (fill in the blank) for the user to log on, and other placeholders for saving the user's images and important information. ThisTemplate-- This is named because it provides the page display mode-only for designers, who control the overall appearance of the page and leave placeholders for names, images, and other data. The Code only provides data for placeholders. The tasks of developers are still focused on computing.

Of course, the form and function must work together. If the template expects an amount in USD, the Code should not provide a URL. If the template expects to obtain objects, the Code should not provide a list. Therefore, the template system must separate the form and function, but must also establish a connection between the two.

The most popular Web Application Programming Languages (Perl, Python, Ruby, Java)™), And PHP is no exception. In the search engine, TypePHP template engine, You may find more than 25 options (for a list that highlights each of The Engine Features under study, The PHP Template Engine Roundup, see references ).

Some PHP template engines have optimized the speed. Other PHP template engines are designed to encourage simplified use of separated forms and functions. In some packages, Placeholders are described in PHP, and other solutions have a customized short programming language. The selection of a template engine depends largely on the requirements, so it is suitable for a small amount of research and experimentation.

Here, I will introduce you to Smarty, one of the most popular PHP template engines. The Smarty "code" has its own syntax and operator extended list, but the system is not hard to learn. Read or browse the Smarty document to familiarize yourself with all its functions. Starting with the small changes to Smarty, you can expand your skills as needed and become more proficient.

Get Smarty

The Smarty Web site maintains an active email list, a support forum, and an Internet Relay Chat (IRC) Forum (see references ). Development is in progress, and this article is based on V2.6.18, which was released on July 18, March 7, 2007.

Smarty has two aspects: PHP application programming interface (API) and Display Engine. The application code calls the API to associate the code variables with the template placeholder. The Display Engine interprets the Smarty tag, execution loop, reference placeholder, and display final results. Smarty functions include:

<

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.