Smarty Example Tutorial-Template Design Section

Source: Internet
Author: User
Tags header html page php file php and php code php template smarty template

One, what is Smarty?

Smarty is a PHP template engine that is written out of the template, it provides a separation of logic and external content, simply, the goal is to use the PHP programmer to separate from the art, the programmer to change the logical content of the program will not affect the graphic design of the page, The art of modifying the page does not affect the program logic of programs, which in many people cooperation projects

is particularly important.

Second, Smarty Advantages:

1. Speed: A program written with Smarty can achieve maximum speed, which is relative to other template engine technology.

2. Compile: The program written by Smarty to compile into a non-template technology php file, this file uses a mixed PHP and HTML, the next time the template to access the Web request directly to the file, And no more template recompilation (without changes to the source program)

3. Caching technology: A caching technique chosen by smarty, which caches the HTML files that the user eventually sees as a static HTML page, and when the cache property of the set Smarty is true, Converting a user's Web request directly into this static HTML file during the Cachetime period of the Smarty setting is equivalent to calling a static HTML file.

4. Plug-in technology: Smarty can customize Plug-ins. Plug-ins are actually some custom functions.

5. If/elseif/else/endif can be used in templates. Using Judgment statements in template files makes it easy to rearrange templates.

Third, not suitable to use Smarty place:

1. Content that needs to be updated in real time. For example, like the stock display, it needs to update the data frequently, and this type of program using Smarty will slow down the template processing speed.

2. Small projects. Small projects because the project is simple and art and programmer with a person's project, the use of Smarty will lose the advantages of PHP development quickly.

Four, installs the Smarty class:

Install Smarty Environment: PHP version 4.06 and above.

Installing the Smarty method is very simple and downloads smarty from the http://samrty.php.net. All files in Libs

Copy into the Comm directory and complete the basic installation.

Other advanced installation methods please see the manual.

V. Use of smarty in the template:

This section uses several examples to talk about the use of smarty. The Smarty template is usually identified by the. TPL, and some people write the extension directly for the sake of art. HTML is also possible. In this paper, the Smarty standard is used to form: the. TPL is represented as a smarty template.

PHP Code:--------------------------------------------------------------------------------

Example 1:

Let's look at a simple example.

=====================================================

Index.tpl

=====================================================

{* Display is smarty variable character with * included text for annotation content *}

{include file= "HEADER.TPL"} {* Page Header *}

Hello, my name is {$name}, you are welcome to read my Smarty learning materials.

{include file= "FOOT.TPL"} {* Page tail *}

This example above is a TPL template, where:

1. {* *} is a comment on the template page that does not produce any output when smarty the template, only for template designers to annotate the template.

2. {Include file= "XXX.TPL"} Use this sentence to include a template file in the current page, where the HEAD.TPL and foot.tpl of common things in the site are included and you can

In this way, you use this sentence to copy the contents of the XXX.TPL to the current statement. Of course, you can also copy the contents of the XXX.TPL to the current statement without using this sentence.

It's perfectly OK.

3.{$name}: Template variable, smarty in the core composition, using the smarty definition of the left border character {and the right boundary character} contains, in PHP variable form, in the Smarty program will use

$smarty->assign ("name", "Li Xiaojun"), and replace $name in the template with "Li Xiaojun" three words.

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.