Basic concepts of PHP template technology

Source: Internet
Author: User
Tags html page variables php file php and php script php template variable
Concept | Templates If you are designing an interactive website, you will certainly focus on two major issues, namely art and programs. This is also a Web site in the construction of its content after the most critical elements. There are usually two ways to reconcile the relationship between art and programs:

1. Do the art page first, then the programmer directly in the art page HTML file embedded asp, JSP, PHP and other program code.

2. Art and program at the same time, but because there is no page frame, the program can only make some key code, the two sides completed after the art page and program code embedded synthesis.

In the actual site construction process, due to people, progress and other environmental constraints, you will often mix the above two coordinated approach. However, both approaches are deficient:

1. Efficiency is not high. The coordination between the two is not likely to produce wait, repeat code debugging steps, and other phenomena;

2. Debugging is not smooth. Because the program code ultimately needs to embed in the HTML page, the code embedding, debugging, error correction are more cumbersome;

3. Maintenance inconvenience. Once the art design needs to be modified, such as the site revision, then all the programs and HTML code mixed pages need to rewrite;

If you are using a PHP program to build a website, then congratulate you, PHP template technology will be more satisfactory solution to the above problems.

So what is the template technology for PHP? The PHP template, the Phplib template technology, is one of the main modules in the Phplib Library, developed from the template of Perl. And Phplib is an extension in PHP, provides a lot of class library, can easily realize some basic functions such as user authentication, database encapsulation and so on. We can download the latest version of the phplib.netuse.de. To use a php template, just unpack the Template.inc file in the Phplib package and put it in the directory that our PHP program can call.

To peel away the veil of mystery, the core concept of template technology is simply a heartbeat: to designate your Art page as a template file, simply place the contents of the activity in the page, such as the database output, the user interaction, and so on as a variable in the form {variable} in the appropriate location in the template file, and when the user browses, The template file is opened by the PHP program file, and the variables defined in the template file are replaced, of course, replaced by the corresponding database output or the user interaction dynamically generated content, for example:

Define template file: mytemplate.html

The contents of the mytemplte.html are:


.....

<body>

...

...

</body>


As we can see, the template file is actually an ordinary HTML file that contains the elements of the layout and artwork you want, while the internal activities are in the form of variables and waiting to be replaced. Obviously, the template file is directly browsed is meaningless, because it does not contain any PHP program, all the content is "dead", now we look at how to call the template, let it "activity" up.

Suppose we use the above template in the test.php file, and when the user browses the test.php file, the test.php file processing process is as follows:

test.php

<?php

Mydate=date ("Y-year m-month D-Day"); Handle the value of a variable

Include ("Template.inc"); To transfer the template program module

Mytemp = new Template ("/Your template file directory/");

Create a template instance mytemp, if the template is in the current directory, then use the "." Will be

Mytemp->set_file ("Myfilehandle", "mytemplate.html");

Set the Myfilehandle file handle to the template file that we want to call mytemplate.html

Mytemp->set_var ("Today", mydate);

Call the Set_var method of the template, and set the variable today value in the template to $mydate

Mytemp->parse ("Myoutput", "Myfilehandle");

Invokes the parse method of the template, analyzes the template file that the Myfilehandle file handle points to, and replaces the corresponding variable

The complete HTML file content is saved in the string variable myoutput.

Mytemp->p ("Myoutput");

The value of the myoutput of the printed output string variable

?>

It is so simple, template technology is very easy to separate the design of the site into a clear art and program two aspects, and their results simple and easy to combine. If the art design needs to be modified, then we just remember the original template variable names, insert them into the new template corresponding position on it. It's really easy! On the other hand, in the PHP program basically does not have the PHP script and the HTML syntax mixes the phenomenon, greatly enhances the PHP system the execution efficiency, moreover if is the database and so on some time-consuming operation, now may close the database in time, releases the resources, This also improves the efficiency of resource usage in PHP and related systems to some extent.

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.