Smarty instance tutorial
--- Program Design Part
In the template design of Smarty, I simply introduces SMARTY some common settings in the template. This section mainly introduces how to start our program design in smarty.
First introduce some of the elements in the PHP file we use in the previous section. Similarly, we take the beginning of the index.php file in the first section to explain:
=================================================
Index.php
=================================================
PHP
/ ***************************************************
*
* File name: index.php
* Decision: Display instance program
*
* Servers: Master
* Email:
Teacherli@163.com
*
*************************************************** /
INCLUDE_ONCE ("./ Comm / Smarty.class.php"); // contains the smarty file
$ smarty = new smarty (); // Establish a smarty instance number $ smarty
$ smarty-> template_dir = "./Templates";// set template directory
$ smarty-> compile_dir = "./Templates_c"; // Set the compilation directory
// **** Everyone pays attention, here I am my newly added **** //
$ TPL-> cache_dir = "./cache"; // Set the cache directory
$ smarty-> cache_lifetime = 60 * 60 * 24; // Set the cache time
$ Smarty-> CACHING = true; // Set the cache method
/ / -------------------------------------------------------------------------------------------- ----
// Left and right boundary, the default is {}, but it is easy to use JavaScript in the practical application.
// Conflict, so it is recommended to set <{}> or other.
/ / -------------------------------------------------------------------------------------------- ----
$ Smarty-> LEFT_DELIMITER = "<{";
$ smarty-> right_delimiter = "}>";
$ Smarty-> Assign ("Name", "Li Xiaojun"); / / Template variable replacement
// Compile and display the INDEX.TPL template under ./Templates
$ Smarty-> Display ("INDEX.TPL");?>
We can see that SMARTY's program part is actually in line with a group of PHP language specification, let's explain it in turn:
1. / ** / statement:
The part is included as a program header. The main content should be a simple introduction to the role of the program, copyright and authors and time, which is not required in Smarty, but from the style of the program, this is a good style.
2. INCLUDE_ONCE statement:
It contains the Smarty file installed to the website to the current file, and note that the path containing must be written correctly.
3. $ smarty = new smarty ():
This sentence has created a smarty object $ smarty, an instantiation of an object.
4. $ Smarty-> Template_dir = ":
This sentence indicates the path when the $ smarty object uses the TPL template. It is a directory. When there is no such, the SMARTY default template path is the Templates directory of the current directory. When we do this, we must write this sentence. This is also a good program style.
5. $ smarty-> compile_dir = "":
This sentence indicates a directory of the $ smarty object to compile. In the template design articles we already know that smarty is a compilation template language, and this directory is its compilation template directory. Here, pay attention to the site is on the * NIX server, make sure that this directory defined in compile_dir has a handle Ready permissions, by default it is a Templates_c in the current directory, which is clearly written out for the same reason.
6. $ Smarty-> Left_DELIMITER and $ Smarty-> Right_Delimiter:
Indicates the left and right divisions when looking for template variables. By default, "{" and "}", but in practice because we want to use