This article describes how to quickly get started with smarty, allowing readers to quickly master the usage of smarty in half an hour. Share it with everyone for your reference. The specific implementation method is as follows:
1. Smarty programming part:
In the smarty template design section, I briefly introduced some common settings of smarty in the template. This section mainly introduces how to start our program design in smarty. Download the Smarty file and add it to your site.
index.php code is as follows:
$smarty = new Smarty(); //Create smarty instance object $smarty
$smarty->templates("./templates"); //Set the template directory
$smarty->templates_c("./templates_c"); //Set the compilation directory
$smarty->cache("./cache"); //Cache directory
$smarty->cache_lifetime = 0; //Cache time
$smarty->caching = true; //caching method
$smarty->left_delimiter = "{#";
$smarty->right_delimiter = "#}";
$smarty->assign("name", "zaocha"); //Replace template variables
$smarty->display("index.htm"); //Compile and display the index.htm template located under ./templates
?>
We can see that the program part of smarty is actually a set of codes that conform to the PHP language specification. Let’s explain them in turn:
1:/**/Statement:
The included part is the program header comments. 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 from 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 of the $smarty object when using the tpl template. It is a directory. Without this sentence, Smarty's default template path is the templates directory of the current directory. When actually writing the program, we need to change this sentence Note that 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. Please note that if the site is located on a Linux server, please make sure
The directory defined in teamplates_c has writable and 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 separators when searching for template variables. By default, it is "{" and "}", but in practice, because we need to use