include_once("./comm/Smarty.class.php"); //Include smarty class files
$smarty = new Smarty(); //Create smarty instance object $smarty
$smarty->templates("./templates "); //Set the template directory
$smarty->templates_c("./templates_c"); //Set the compilation directory
//****Attention everyone, I am the new member here****//
$smarty->cache("./ cache"); //Set the cache directory
$smarty->cache_lifetime = 60 * 60 * 24; //Set the cache time
$smarty->caching = true; //Set the caching method
//---- ---------------------------------------------
/ /The left and right boundary characters, the default is {}, but in actual application it is easy to conflict with JavaScript
//, so it is recommended to set it to <{}> or other.
//-------------------------------------------------- -----
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
$smarty->assign("name", "Li Xiaojun"); //Replace template variables
//Compile and display the index.tpl template located under ./templates
$smarty->display("index.tpl");
?>
We can see that the smarty program Part of it is actually a set of codes that comply with the PHP language specifications. Let’s explain them in turn:
1. /**/Statement:
The included part is the program header comment. The main content should be a brief introduction to the function of the program, copyright, author and writing time. This is not necessary in smarty, but in terms of the style of the program, this is a good style.
2. include_once statement:
It will include the smarty file installed on the website into the current file. Note that the included path must be written correctly.
3. $smarty = new Smarty():
This sentence creates a new Smarty object $smarty, which is a simple instantiation of an object.
4. $smarty->templates(""):
This sentence specifies the path when the $smarty object uses the tpl template. It is a directory. Without this sentence, Smarty's default template path is the templates
directory of the current directory. The actual directory When writing a program, we should write this sentence clearly. This is also a good programming style.
5. $smarty->templates_c(""):
This sentence specifies the directory where the $smarty object is compiled. In the template design chapter, we already know that Smarty is a compiled template language, and this directory is the directory where it compiles templates. Note here that if the site is located on a *nix server, please ensure that the directory defined in teamsplates_c has a valid Write-readable permissions. By default, its compilation directory
is templates_c in the current directory. For the same reason, we write it out explicitly.
6. $smarty->left_delimiter and $smarty->right_delimiter:
Indicates the left and right delimiters when looking for template variables. By default, it is "{" and "}", but in practice, because we need to use