The installation of PHP learning notes Smarty and the initial initialization example

Source: Internet
Author: User
Tags html page php class php file php and php download php script php template smarty template

Installing Smarty is to include the Smarty class library in your own PHP project. The installation steps are as follows:

(1) need to smarty official website http://www.smarty.net/download.php Download the latest stable version, all versions of the Smarty class library can be used on UNIX and Windows servers.
(2) Then unzip the compressed package, after the release will see a lot of files, which have a name of Libs folder, is a folder containing Smarty class library. Install smarty only need this one folder, other files are not necessary to use.
(3) There should be three class.php files, 1 debug.tpl, 1 plugin folders, and a core folder in the Libs, and the Libs folder will be copied directly to your program's home folder.
(4) In the execution of the PHP script, through the Require () Statement Libs directory Smarty.class.php class files loaded in, Smarty class library can be used.
The installation method provided above is suitable for developers who have been brought over by the program to use, so that there is no need to consider whether the host has installed Smarty.
Initialize the default settings for the Smarty class library
By using the previous introduction to the Smarty Class library installation, the Require () method is invoked to include the Smarty.class.php file in the execution script and to create an object for the Smarty class. However, if you need to change the default values for some members of the Smarty class library, not only can you modify them directly in the Smarty source file, you can also set new values for Smarty objects after you create the Smarty object.

The code is as follows Copy Code

<?php
The instantiation of/**file:init.inc.phpsmarty object and the initial document * *

Define ("ROOT", Str_replace ("\", "/", DirName (__file__)). '  /'); Specify the root path of the project
Requireroot. ' libs/smarty.class.php '; Loading Smarty class files

$smarty = Newsmarty (); Instantiate the object of the Smarty class $smarty

/* Recommended to use the Smarty3 version of the default path, the successful return of the $smarty object itself, can be consistent operation.
$smarty->settemplatedir (root. ' templates/')//set directory for all template files
->addtemplatedir (root. ' templates2/')//can add more than one template directory (front backstage one)
->setcompiledir (root. ' templates_c/')//set directory of all compiled template files
->setpluginsdir (root. ' plugins/')//Set directory for template extensions
->setcachedir (root. ' cache/')//set directory where cache files are stored
->setconfigdir (ROOT. ' Configs '); Set up a directory where template profiles are stored

$smarty-> auto_literal = false; You can let the bounding symbol use a space
$smarty->caching = false; Set the smarty cache switch function
$smarty->cache_lifetime = 60*60*24; Set the length of the template cache valid period to 1 days
$smarty->left_delimiter = ' <{'; Set the left end character in the template language
$smarty->right_delimiter = '}> '; Set the right end character in the template language
?>

Additional Choice Smarty Template engine

For PHP, there are many template engines to choose from, but Smarty is written in PHP and is the most famous and powerful PHP template engine in the industry today. Smarty like PHP has a rich library of functions, from counting words to automatic indentation, text wrapping and regular expressions can be used directly, if not enough, Smarty also have a strong ability to expand, through the form of plug-ins can be expanded. In addition, Smarty is also a free software that allows users to freely use, modify, and redistribute the software. The advantages of Smarty are summarized as follows.
Speed: In contrast to other template engine technologies, programs written with Smarty can achieve maximum speed.
Compiled: A program written in Smarty to compile into a template-less PHP file that uses PHP and HTML to convert the Web request directly to this file the next time the template is accessed, instead of recompiling the template ( With no changes to the source program, the subsequent calls are faster.
Caching Technology: Smarty provides an alternative caching technique that caches the HTML files that the user eventually sees as a static HTML page. When the user turns on the smarty cache, and within the set time, the user's Web request is converted directly into the static HTML file, which is equivalent to calling a static HTML file.
Plug-in technology: The Smarty template engine is implemented using the object-oriented Technology of PHP, which can be modified not only in the original code, but also in a number of functional plug-ins (that is, some custom functions are defined by rules).
Powerful presentation logic: the ability to handle data through conditional judgments and iterations in a smarty template is actually a programming language, but the syntax is simple, and designers can learn quickly without having to prepare for programming knowledge.
Of course, there are also places that are unsuitable for using smarty. For example, the content that needs to be updated in real time requires that the template be recompiled frequently, so this type of program uses Smarty to slow down the template processing speed. In addition, in the small project is also not suitable for the use of Smarty templates, small projects because of the simple project and the programmer and a person's project, the use of smarty will be to some extent lost the advantages of PHP development quickly.

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.