six. Smarty programming section: In the template design of Smarty, I simply introduce SMARTY some common settings in the template. This section mainly introduces how to start our program design in Smarty.
PHP code:
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-> Templates ("./ Templates"); // Set the template directory
$ Smarty-> Templates_c ("./ Templates_c"); // Set the compilation directory
// **** Everyone pays attention, here I am my newly added **** //
$ smarty-> cache ("./ 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 writing time, which is not a must
This is a good style from the style of the program.
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-> Templates (""):
This sentence indicates the path when the $ smarty object is using the TPL template. It is a directory. When there is no such sentence, the smarty default template path is the Templates of the current directory.
Directory, when you write programs, we have to write this sentence, which is also a good program style.
5. $ Smarty-> Templates_c (""):
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 compiled.
Template's directory, hereby pay attention, if the site is on the * NIX server, make sure that this directory defined in Teamples_C has writable readable permissions, by default it compiled
It is the Templates_c under the current directory, and we will clearly write it 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