PHP smarty Template First Experience _php tutorial

Source: Internet
Author: User
Tags smarty template
The following describes the features of the Smarty template engine:

1. Speed: The program written in Smarty can get the maximum speed improvement, which is relative to other template engine technology.

2. Compiled: The program written in Smarty is compiled into a non-template PHP file at runtime, this file is a mixture of PHP and HTML, the next time you access the template, the Web request directly into this file, Instead of recompiling the template (in case the source program has not changed)

3. Caching technology: Smarty chooses a caching technology that caches the HTML files the user eventually sees as a static HTML page, and when the cache property of the Smarty is set to True, The user's Web request is converted directly to this static HTML file during the Cachetime period set by Smarty, which is equivalent to calling a static HTML file.

4. Plug-in technology: Smarty can customize the plugin. Plugins are actually some of the custom functions.

5. If/elseif/else/endif can be used in the template. The template file can be easily reformatted by using a judgment statement.

Using Smarty template version Smarty-3.0.8, unzip the file directory as follows:

So I started my smarty trip.

  Step 1

Create a new Smartytest folder in the Server Web page folder, take only the files in the Libs directory, copy them to the Smartytest folder, and rename them to Smarty

  Step 2

Create a new directory in test templates, and create a new four folder in the directory cache, configs, templates, Templates_c, the Completed folder form as

  Step 3

Write a configuration file, through which you can implement the connection with Smarty, and write it as a separate file can be written on different pages and repeat the same code (it can also be written in class form, easy to customize), here I have its file name as config.php
Copy CodeThe code is as follows:
Gets the absolute path where the current folder is located H:\wamp\www\smartytest\
Define (' Smarty_path ', substr (DirName (__file__), 0,-9));
Gets the absolute path to the Templates folder H:\wamp\www\smartytest\templates
Define (' Templates_path ', Smarty_path. ' templates/');
Require Smarty_path. ' smarty/smarty.class.php ';
$smarty = new Smarty;
Defining directory Paths
$smarty->template_dir = templates_path. ' templates/';
$smarty->complile_dir = templates_path. ' templates_c/';
$smarty->config_dir = templates_path. ' configs/';
$smarty->cache_dir = templates_path. ' cache/';
Define left and right terminator {% and%}
$smarty->left_delimiter = ' {% ';
$smarty->right_delimiter = '%} ';
Turn off caching
$smarty->caching = false;
Turn off debugging
$smarty->debugging = false;
?>

Step 4
Write a simple template file named Index.tpl, and put it in the Templates\templates directory
Copy CodeThe code is as follows:




Smarty

{% $hello%}


Step 5
Write a php file named index.php, placed in the templates file directory
Copy the Code code as follows:
Require ' config.php ';
$smarty->assign (' Hello ', ' Hello Word ');
$smarty->display (' Index.tpl ');
?>

Now the file directory is

  Step 6

Test file:

  

Summary: The use of smarty template process also met the problem, such as Smarty's directory can set their own, the customization is strong, so there are many versions of the Smarty tutorial (I see a version in the book, Baidu Encyclopedia also has another), the results of two are reference to do not understand how to put, The last is to use the kind of Baidu encyclopedia (that is, the above), followed by the Baidu Encyclopedia code is copied over, the results in the use of syntax error, unexpected t_variable errors, look at the code are right ah, is a mistake, finally find the reason, The original is the full-width space in the Web page, so the wrong way to copy the code on the Web page the best method is to re-write the blanks; Note that when you write a php file, hello does not have a $ symbol in front of it, and you must add the $ symbol when referencing it in a TPL file.

http://www.bkjia.com/PHPjc/324022.html www.bkjia.com true http://www.bkjia.com/PHPjc/324022.html techarticle The following describes the features of the Smarty template engine: 1. Speed: The maximum speed can be improved with a program written in Smarty, which is relative to other template engine technologies. ...

  • 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.