Php-Smarty-1 (18), phpsmarty-118_PHP tutorial

Source: Internet
Author: User
Tags php template
Php-Smarty-1 (18), phpsmarty-118. Php-Smarty-1 (18), phpsmarty-118smarty 1, template principle 1, why use template 1) scenario: for ($ i0; $ I $ count; $ I ++) {echotd. $ row [proname]. td} above this writing will be php-Smarty-1 (18), phpsmarty-118

Smarty

I. Template principles

1. Why use a template?

1) scenario:

For ($ I = 0; $ I <$ count; $ I ++ ){

Echo''. $ Row ['proname'].'

}

The above code is used to mix html and php, which is not conducive to development and maintenance.

2) scenario:

We have developed a website. after a period of operation, the customer is not satisfied with the front-end. we have developed another interface. However, after the operation, the customer thinks that the previous style is better, frequent changes to website styles

The best way to solve the above two problems is to use the template

2. implementation of the template

The template is actually a string replacement.

1) Example 1

Template File:

Php file:

1) Example 2:

Template class:

PHP code:

The code in the above class file is not optimized enough. we can use a popular template framework to implement our own project.

II. Introduction to Smarty

1. what is Smarty?

Smarty is a PHP template engine developed based on PHP. It provides the separation of logic and external content.

2. Smarty features

L speed

L compilation type

L cache technology

L plug-in technology

L free statement if/elseif/else/endif

L Real-time update: high real-time requirements

3. configuration

Http://smarty.php.net

L/lib

L Smarty 2.6 php4.0

L Smarty 3.0 php5.0

1. decompress the smarty file and enter the decompressed directory.

Demo: Case

Libs: Smarty source code

1) go to the libs Directory,

Plugins: Custom plug-in Directory

Sysplugins: System plug-in Directory

Debug. tpl: there is a debugging function in Smarty. you can observe the variable value and Debug window template.

Smarty. class. php: Smarty core class file (entry file)

SmartyBC. class. php: Smarty3.0 is upgraded based on 2.0. to use the 2.0 feature, you can reference this portal file.

3) copy the libs directory to the project and rename it

3. Quick start

1. code implementation:

Template code:

{$ Name}, {$ age}: Template tag variable

Program code:

2. working principle of smarty:

L smarty first compiles the php source file into an intermediate file

L If cache is enabled, the cached files are generated based on the compiled files.

L after each access, the compiled files will be accessed

L If cache is enabled and cached files are not expired, access the cached files directly.

When Smarty runs a program for the first time, it generates a compilation file.

In the future, when this program is run, as long as the template is not changed, the compilation file will be run directly. if the template is changed, the compilation file will be generated again and then run.

Compile is a pointer to compile the template.

3. schematic diagram:

Http://www.bkjia.com/PHPjc/993275.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/993275.htmlTechArticlephp-Smarty-1 (18), phpsmarty-118 smarty 1, template principle 1, why use template 1) scenario: for ($ I = 0; $ I $ count; $ I ++) {echo td. $ row [proname]. /td...

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.