Php getting started smarty template learning example (1/5)

Source: Internet
Author: User

I don't know when to start. Someone started
The Server Script embedded in HTML is not satisfied. However, both Microsoft's ASP and open source PHP are embedded Server scripts.
The webpage servo language. As a result, some people think that if the application logic (or commercial application logic) and web page presentation (Layout) can be)
Is logical separation better?

In fact, this problem has existed for a long time. When interactive web pages become popular, whether it is ASP or PHP
The users are both program developers and visual designers. However, these users are usually either program or artist, and if they want to take both of them into account, they have to die a lot of brain cells...

So the template engine came into being! The purpose of the template engine is to achieve the logic separation function mentioned above. It enables program developers to focus on data control or function fulfillment, while visual designers can focus on web page layout to make webpages look more professional! Therefore, the template engine is suitable for the company's website development team, so that everyone can exert their expertise!

For the template engine that I have been familiar with, it is roughly divided into two forms based on the data presentation method: the template engine that needs to be matched with the program processing and the template engine that is completely determined by the template itself.

In the template engine that needs to be matched with the program processing, the program developer must be responsible for the variable presentation logic, that is, he must process the variable content before outputting it to the template.
The work of assign. In other words, program developers should write more programs to determine the style of variables. The template engine, which is completely determined by the template itself, allows variables to be directly assign
To the template, let the visual designer decide how to present the variables when designing the template. Therefore, it may have another set of template program syntax (such as Smarty)
To control the presentation of variables. However, visual designers also need to learn how to use the template language.

The operating principle of the template engine is as follows:

General template engine
(Such as PHPLib) is the template to be parsed when the template object is created. After the variables are embedded, the template is parsed using the parse () method, and then the webpage is output.

Pair
For the Smarty user, the program does not need to do any parse action, and these Smarty will automatically help us. In addition, if the template of a compiled webpage is not changed,
Smarty
Automatically skip the compilation action and directly execute the compiled webpage to save compilation time.

Some concepts of using Smarty

In the general template engine, we often see the concept of a region. The so-called block may grow like this:
<! --
START: Block name -->
Region content
<! -- END: Block name
-->

Most of these blocks will use if or for, while
To control their display status, although the template looks much more concise, but as long as you change the display mode of different templates, the PHP program is bound to be changed again!

In the Smarty
And all the presentation logic is controlled by the template. Because Smarty has its own template language, the template syntax of Smarty is used whether the block is to be displayed or repeated.
(If, foreach, section) with variable content for rendering. In this way, it seems that the template has become a little complicated, but the advantage is that if you plan properly, PHP
You do not need to change the program line.

From the above description, we can know that to use Smarty, we need to master the principle of clearly separating the application logic from the webpage presentation logic. PHP
Do not have too many HTML codes in the program. In the program, you only need to make sure that the variables are in the template, so that the template determines how to present these variables (or even not)
.

Smarty basics

Install Smarty


First, we first determine the place where the program is placed.

In Windows, it may be like this :「
D: appservwebdemo 」.

In Linux, it may be like this: "/home/jaceju/public_html/
」.

Download the latest Smarty kit: http://smarty.php.net from The Smarty official website.

Unlock Smarty
After 2.6.0, you will see many files, including a libs folder. Should there be 3 class. php in libs? N + 1 debug. tpl + 1
Plugin folder + 1 core folder. Copy libs to the main folder of your program and rename it class.
You can. That's it? That's right! This method is relatively simple and suitable for users without their own hosts.

As for Smarty
Why are some complicated installation methods described in the official manual? Basically, the installation is based on the official method. It can be installed only once on the host, and then directly referenced when all designers under the host develop different programs.
Duplicate Smarty. The method provided by the author is suitable for developers who want to bring the program over and migrate it, so there is no need to worry about whether the host has installed Smarty.
.

Program folder settings

Take the installation of Appserv in Windows as an example. The main folder of the program is "d: appservwebdemo 」. After installing Smarty, we will create a folder like this under the main folder:

In
Under Linux, remember to change the templates_c permission to 777. Windows
To cancel the read-only operation.

The first small program written with Smarty

Set the Smarty
Directory. Name the following file as main. php and place it in the main folder:

The code is as follows: Copy code
Main. php:
<? Php
Include
"Class/Smarty. class. php ";
Define ('_ SITE_ROOT', 'd:/appserv/web/Demo ');//
No diagonal lines at last
$ Tpl = new Smarty ();
$ Tpl-> template_dir = _ SITE_ROOT.
"/Templates /";
$ Tpl-> compile_dir = _ SITE_ROOT.
"/Templates_c /";
$ Tpl-> config_dir = _ SITE_ROOT.
"/Configs /";
$ Tpl-> cache_dir = _ SITE_ROOT.
"/Cache /";
$ Tpl-> left_delimiter = '<{';
$ Tpl-> right_delimiter =
'}> ';
?>



Homepage 1 2 3 4 5 Last page

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.