Separating forms and functions from PHP applications with Smarty

Source: Internet
Author: User
Tags php language php code php template reserved smarty template
A
random mix of PHP and other Web page tags will cause program logic, HTML, cascading style sheets (cascading style sheets,css), and JavaScript to be in disarray, making maintenance a daunting task. The Smarty template engine separates the form from the function.
--> --> -->

PHP Web applications are easy to start with. The syntax of the PHP language is neat and easy to master. You can mix PHP directly with HTML, JavaScript, and CSS to quickly generate visual results. Also, deploying your PHP application to your own WEB server or hosting service is a piece of cake.

But mixing PHP with other page tags is also a responsibility. PHP code is often a complex web (not a joke) that contains program logic, Structured Query Language (structured query language,sql) queries, functions, classes, developer comments, HTML, CSS styles, and scripts. Worse, there are many ways to move content from PHP echo to output buffers. Maintaining such a messy page is laborious. Making insignificant changes to code or markup can cause serious damage, and enhancements to the page may require the designer and programmer to work together. Using PHP, the form (layout of the page) and the function (the purpose and construction of the page) will be mixed together.

In an ideal situation, forms and functions are independent of each other. For example, CSS and HTML must be the case. CSS is a form, while HTML is a feature. In the case of PHP, it would be ideal if the page markup and code were to be separated. The code will process input, make decisions and generate display data, and the markup will expect to get the data and provide the required brackets to render the information.

For example, the markup for the home page may leave a fill in the blank for the user to log on, as well as other placeholders to hold the user's images and important information. This template --which is named because it will provide a pattern of page display--for designers only, designers will control the overall appearance of the page and leave placeholders for names, pictures, and other data. The code only provides data for placeholders. The developer's task is still largely focused on computing.

Of course, form and function must collaborate. If the template expects to get an amount in dollars, the code should not provide a URL. If the template expects to get an object, the code should not provide a list. Therefore, the template system must separate the form from the function, but it must also establish a connection between the two.

The most popular Web application programming languages (Perl, Python, Ruby, Java™) all have template engines, and PHP is no exception. Type in the search engine PHP template engine , and then you may find more than 25 options (see Resources for a list of the PHP Template Engine Roundup that emphasize the features of each engine you study).

Some PHP template engines are optimized for speed. Other PHP template engines are designed to encourage the separation of forms from functions while simplifying usage. In some packages, placeholders are described in PHP itself, while other solutions have a custom, short programming language. How to select a template engine depends to a large extent on the requirements and is therefore appropriate for a small amount of research and experimentation.

Here, I introduce you to Smarty, one of the most popular PHP template engines. Smarty "Code" has its own list of syntax and operator extensions, but the system is not difficult to learn. Read or browse the Smarty document to familiarize yourself with all of its features. Start with a small change in Smarty, expand your skills as needed, and then become more proficient.

Get Smarty

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

Smarty has two aspects: the PHP Application Programming Interface (API) and the display engine. The application code associates the code variable with the template placeholder, and the display engine interprets the Smarty tag, execution loop, reference placeholder, and display the final result. Smarty features 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.