Introduction to Smarty Installation and use of PHP templates

Source: Internet
Author: User
Tags execution functions include variables php file php template php web development variable

In the use of PHP to develop large, interactive Web sites, we often encounter with the art of how to cooperate with the problem, usually our solution is to design the page after the designer to develop, and then deliver the artwork to improve the page, back and forth several times, if you encounter the program designer of HTML unfamiliar, It's a painful task for both sides, and it's more inefficient, and it's important to have template support.

We know that the PHP language as a member of the open source community, providing a variety of template engines, such as Fasttemplate,smarty,simpletemplate, and Smarty is now a more used PHP template engine, Today I share with you how to install and use Smarty in PHP development, which is also considered as an introductory study to Smarty.

Preparatory work

1, choose the installation of Smarty directory

If you have server permissions, consider the security option to install Smarty outside the Web program document directory, and then include the include_path options in the php.ini file by including the Smarty installation directory address.

If you have virtual host permissions, or several projects, you can install Smarty in your own project directory, require Smarty class files, or use the Smarty template engine. Of course, for security reasons, you can prohibit directory access via Apache.

In addition, there are differences in portability between the two Smarty installations, the first of which is to ensure that each server has the same smarty configuration, and the second does not affect each server configuration. You can choose the Smarty installation method according to your own needs.

2, download smarty, please click here to download Smarty, I chose is Smarty-2.6.25

Smarty Installation Steps

1, unzip the download Smarty-2.6.25 compression package

2, Copy Libs folder to the Web program directory, my directory for Test\smarty

The installation method under Linux can refer to here.

After installing the Smarty template, we started to use the smarty simply.

Smarty Use

1, create the relevant directory

Because in the process of using smarty, Smarty will generate the compiled template files and other configuration files, cache files, we need to create the relevant directories. I created the Tpls directory under the Test\smarty directory and created templates, Templates_c, configs, and cache directories under the Tpls directory. Why do you need to create these directories? With the Smarty.class.php file open, we can see that the Smarty class defines part of the member property.

  $template _dir: Sets the directory address where all template files need to be placed. By default, the directory is: "./templates", which is to find the template directory under the same directory as the PHP execution program.

  $compile _dir: Sets the directory address of all template files that Smarty compiled. The default directory is: "./templates_c", which is to look for the compiled directory under the same directory as the PHP execution program. If you create this directory on a Linux server, you will also need to modify the permissions of this directory so that it has write permissions.

  $config _dir: Set up a directory for storing template special profiles, the default directory is: "./configs", which is to find the configuration directory under the same directory as the PHP execution program.

  $cache _dir: In the event that the cache feature is started, all templates in Smarty cache are placed in the directory specified by this property. The default directory is: "./cache", which is to find the cache directory under the same directory as the PHP execution program. You can also use your custom cache handler to control cache files, which will ignore this setting. Also, if you create this directory on a Linux server, you will also need to modify the permissions of this directory so that it has write permissions.

For system security and portability considerations, it is recommended that you do not build these directories in the same directory as the PHP executable, you can build it in the PHP executable directory, if it has been established in the PHP execution program under the same directory, you can do directory restrictions through Apache access work.

2, establish the relevant configuration file

We need to create a configuration file that overrides the default member properties of the Smarty class and is named Main.php, saved in the Smarty directory, and which script needs to use Smarty in the future, we just include the main.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
?
Include ("./smarty/libs/smarty.class.php");
Define (' Smarty_root ', './smarty/tpls ');
$TPL = new Smarty ();
$tpl->template_dir = smarty_root. " /templates/";
$tpl->compile_dir = smarty_root. " /templates_c/";
$tpl->config_dir = smarty_root. " /configs/";
$tpl->cache_dir = smarty_root. " /cache/";
$TPL->caching=1;
$TPL->cache_lifetime=60*60*24;
$tpl->left_delimiter = ' <{';
$tpl->right_delimiter = '}> ';
?>

Note :
第1-8: The main definition of a Smarty object, while setting template files, compiled files, cached files, configuration file directory, overwriting the Smarty.class.php in the default values.

第9-10: Set the cache to open, and set the cache to a valid time of 1 days.
Knowledge Point : $caching is used to set whether caching is enabled. The default value is set to 0 or invalid. You can also have multiple caches for the same template, starting the cache at 1 or 2 o'clock. 1 tells Smarty to use the current $cache_lifetime variable to determine whether the cache is out of date. 2 tells Smarty to use the Cache_lifetime value when generating the cache. It is recommended that the cache be turned off during project development to set the value to 0

第11-12: Set the left and right end of the Smarty language, and we know that curly braces are the default delimiters for Smarty, but when combined with JavaScript, CSS, and so on, it can be a conflict, so here we set it to <{and}>.

3. Create a template file

In general, the design of the art page after the intersection point is a template file, the two sides agreed, programmers do not need to spend too much energy in the foreground, this is the use of Smarty template engine development benefits.

Let's start with a simple template file called Leapsoul.tpl, where you can add a Smarty variable to the HTML file and save the file as a TPL type file.

1
2
3
4
5
6
7
8
9
10
11
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>
<{$title}>
</title>
<body>
<{$content}>
</body>

Note : The title and content two Smarty variables are set in this TPL file, the file is saved as LEAPSOUL.TPL, and it is saved in the Test\smarty\tpls\templates template file directory.

4, the establishment of application files

The template file is similar to a presentation layer, and after you have created it, we need an application to drive the presentation layer, and the application file is defined as smarty.php.

1
2
3
4
5
6
7
8
9
?
Include ("smarty/main.php");

$tpl->assign ("title", "Leapsoul.cn for you to show Smarty template Technology");

$tpl->assign ("Content", "leapsoul.cn through the detailed installation steps to show you smarty template Technology");

$tpl->display ("Leapsoul.tpl");
?>

Note :

In this code we mainly use two functions in Smarty assign and display,assign you can understand to assign values to variables, display is mainly used to output Web pages. More Smarty functions will be introduced in detail in the future.

Other Notes

Since we turned on caching, interested friends can open the cache and Templates_c,cache directory to store the template's cached files, the beginning of the file has cached information, such as the file generation time and expiration time, and other and general HTML file not much difference, and Templates_c stored the template compiled php execution file.

Here is a simple introduction to the Smarty Template application example even if the introduction is complete.

  Note : PHP Web Development Tutorials-leapsoul.cn Copyright, reproduced in the form of links to indicate the original source and this statement, thank you.







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.