PHP smarty Template First Experience _php template

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

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.

Using the Smarty template version Smarty-3.0.8, the extracted files directory is 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, and copy them to the Smartytest folder, renamed Smarty

   Step 2

Create a new directory templates in test, and create four new folders in this directory cache, configs, templates, Templates_c, the form of a folder as shown below

   Step 3

Write a configuration file, through which you can connect to smarty, and write it as a separate file can be written on different pages of the same code to write the same (of course, it can be written as a class form, easy to customize), here I put it as a file name config.php

Copy Code code as follows:

<?php
Gets the absolute path where the current folder is located H:\wamp\www\smartytest\
Define (' Smarty_path ', substr (DirName (__file__), 0,-9));
Get 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, put it in the Templates\templates directory
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<metahttp-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<title>Smarty</title>
<body>{% $hello%}</body>

Step 5
Write a php file named index.php and put it in the templates file directory
Copy Code code as follows:

<?php
Require ' config.php ';
$smarty->assign (' Hello ', ' Hello Word ');
$smarty->display (' Index.tpl ');
?>

Now the file directory is

   Step 6

Test files:

  

Summary: The use of the Smarty template has also encountered problems in the process, for example, Smarty can set up their own directory, the custom is strong, so there are many versions of the Smarty tutorial (I see a version of the book, Baidu Encyclopedia also has another), the results of the two reference will not understand how to put the Finally, the use of Baidu Encyclopedia of the kind (that is, said above), followed by the code in the Baidu encyclopedia is copied over, the results in the use of syntax error, unexpected t_variable error, looking at the code is very right Ah, is an error, and finally find the reason, The original page is caused by the full corner space, so the code copied on the Web page of the inexplicable error is the best way to write the blanks are written; note that when writing a PHP file, hello does not have the front $ symbol, and in the TPL file reference must be added $ symbol.

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.