Home >Backend Development >PHP Tutorial >Smarty quick start tutorial in half an hour, smarty quick start_PHP tutorial
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 <script> in the template, the function definition in Script will inevitably use {}. Although it has its own solution, it is customary Let’s redefine it</script>
is "{#" and "#}" or "" or other identifiers. Note that if the left and right separators are defined here, in the template Correspondingly, each variable in the file must use the same symbol as the definition, for example, designated as "" here, and
in the htm template.Correspondingly, change {$name} to so that the program can correctly find the template variable.
7:$smarty->cache("./cache"):
Tell Smarty the location of the output template file cache. In the previous article, we knew that the biggest advantage of Smarty is that it can be cached. Here is the directory where the cache is set. By default, it is the cache directory in the current directory, which is equivalent to the templates_c directory. In Linux systems
We want to make sure it is readable and writable.
8:$smarty->cache_lifetime = 60 * 60 * 24:
The cache validity time will be calculated in seconds. The cache will be rebuilt when the Smarty cache variable is set to true when the first cache time expires. When its value is -1, it means that the established cache never expires; when it is 0, it means that the cache is cached every time the program is executed
The save is always re-created. The above setting means setting cache_lifetime to one day.
9:$smarty->caching = 1:
This attribute tells Smarty whether to cache and how to cache. It can take 3 values, 0: Smarty default value, indicating that the template will not be cached; 1: indicating that Smarty will use the currently defined cache_lifetime to decide whether to end the cache; 2: indicating
Smarty will use the cache_lifetime value when the cache is created. It is customary to use true and false to indicate whether to cache.
10:$smarty->assign("name", "zaocha"):
The prototype of this number is assign(string varname, mixed var), varname is the template variable used in the template, var points out the variable name to be replaced by the template variable; its second prototype is assign(mixed var), we The use of this member function will be explained in detail in the following examples. assign is one of the core functions of Smarty, and it must be used for all substitutions of template variables.
11:$smarty->display("index.tpl"):
The prototype of this function is display(string varname), which is used to display a template. To put it simply, it will display the analyzed and processed templates. There is no need to add a path to the template file here, just use a file name. We have already defined its path in $smarty->templates(string path) .
After the program is executed, we can open the templates_c and cache directories in the current directory, and you will find that there are some %% directories below. These directories are Smarty’s compilation and cache directories. They are automatically generated by the program. Do not directly Make modifications to these generated files.
Above I briefly introduced some commonly used basic elements in the Smarty program. In the following examples, you can see that they will be used multiple times.
3. Template description
Next, we will introduce a section loop block and a foreach loop block. Originally they should belong to the template part, but since they are the essence of smarty and are very closely related to the smarty programming part, they will be discussed separately in this section. one time.
1: foreach: used to loop simple arrays. It is a selective section loop. Its definition format is:
Template file:
example.htm page is as follows:
{#foreach item=new from=$news#}
News number: {#$new.id#}
News content: {#$new.title#}
{#foreachelse#}
No news output in database!
{#/foreach#}
{foreach from=$newsArray item=newsID}
News ID: {$newsID}
News content: {$newsTitle}
{foreachelse}
Sorry, there is no news output in the database!
{/foreach}
Program file: example.php is as follows:
News number: 1
News content: News No. 1
News number: 2
News content: News No. 2
News number: 3
News content: News No. 3
News number: 4
News content: News No. 4
News number: 5
News content: News No. 5
News number: 6
News content: Article 6
Foreach can also use foreachelse to match. Use foreachelse to indicate the operation to be performed by the program when the array passed to foreach is null. For specific usage, please refer to the manual.
2. section:
section was created to solve the shortcomings of foreach. Like foreach, it is used to design loop blocks in templates. It is more complex and can greatly meet the needs of the program, so I am used to using it in the program. Without foreach, the basic prototype is:
The parameters are explained as follows:
name: the name of the section, no need to add $
$loop: The variable to be looped. Use assign to operate this variable in the program.
$start: The subscript to start the loop. The loop subscript starts from 0 by default
$step: the increment of the subscript during each loop
$max: maximum loop subscript
$show: boolean type, determines whether to display this block, the default is true
There is a noun that needs explanation here:
Cyclic subscript: Its actual English name is index, which means index. Here I translate it into "subscript", mainly for easier understanding. It represents the current loop index when displaying this loop block. It starts from 0 by default and is affected by $start. If $start is set to 5, it will also start counting from 5. We have used it in the template design part. This It is an attribute of the current {section}. The calling method is Smarty.section.sectionName.index. The sectionName here refers to the name attribute in the function prototype.
The attribute values of the{section} block are:
1. index: The "loop index" we introduced above, the default is 0
2. index_prev: the previous value of the current index, the default is -1
3. index_next: the next value of the current index, the default is 1
4. first: Whether it is the first cycle
5. last: whether it is the last loop
6. iteration: number of loops
7. rownum: current row number, another alias of iteration
8. loop: The last loop number, which can be used to count the number of loops in the section after the section block
9. total: the number of loops, which can be used to count the number of loops after the section block
10. show: It is included in the function declaration and is used to determine whether the section is displayed
You can refer to the manual for their specific attributes. You can use these attributes flexibly in the program. I have used the index attribute in the template part. You can go back and take a look.
Similarly, {section} can also be used in conjunction with {sectionelse} to indicate the processing of the template when the incoming array variable is empty.
We use {section} instead of {foreach} in the above example to achieve the current function. Note that in this example I only use {section} to implement {foreach} in the tpl template. PHP program There are no changes in the file, and the {sectionelse} processing block is added:
example.tpl template file is as follows:
{section name=loop loop=$News}
News number: {$News[loop].newsID}
News title: {$News[loop].newsTitle}
{sectionelse}
Sorry, no news input!
{/section}
example.php file is as follows:
*
* File name: example7.php
* Function: Display example program 2
*************************************************/
include_once("./comm/Smarty.class.php");
$smarty = new Smarty();
$smarty->templates("./templates");
$smarty->templates_c("./templates_c");
$smarty->cache("./cache");
$smarty->cache_lifetime = 0;
$smarty->caching = true;
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
$array[] = array("newsID"=>1, "newsTitle"=>"News No. 1");
$array[] = array("newsID"=>2, "newsTitle"=>"News No. 2");
$array[] = array("newsID"=>3, "newsTitle"=>"News No. 3");
$array[] = array("newsID"=>4, "newsTitle"=>"News No. 4");
$array[] = array("newsID"=>5, "newsTitle"=>"News No. 5");
$array[] = array("newsID"=>6, "newsTitle"=>"News No. 6");
$smarty->assign("newsArray", $array);
//Compile and display the index.tpl template located under ./templates
$smarty->display("example.tpl");
?>
News number: 1
News content: News No. 1
News number: 2
News content: News No. 2
News number: 3
News content: News No. 3
News number: 4
News content: News No. 4
News number: 5
News content: News No. 5
News number: 6
News content: News No. 6
$loopName[name].var is sufficient. loopName is the variable name assigned at the loop, [name] is the string assigned at the name, and the latter is what you want to set in the program array. The subscript name corresponding to the value will do.
Okay, that’s it for this article about smarty programming learning guide. For general applications, this knowledge is enough. For other advanced techniques, please refer to the examples in the manual. In addition, there are also related topics about Smarty. Examples in practical applications include the built-in mysql statement in php, the DB class in phplib, ADODB, the DB class in Pear, etc. Interested friends can pay attention to related content.
I hope this article will be helpful to everyone’s PHP programming design.
First, set the three directories with
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates_c/';
$smarty- >cache_dir = './cache/'; And make sure these three directories exist and the paths are correct.
Secondly, your template is written incorrectly
should be changed to
News number: {$newsID. newsID}
News content: {$newsID.newsTitle}
Cannot "read" the resource index.htm
Smarty failed to read template action for 3 reasons
1.index.htm does not have read permission. See if you are a newbie, you should eliminate this reason under win
2. No template file exists, index.htm does not exist, you know this yourself, you must have created such a template
3. The template path is wrong, this is your crux
When you $tpl = new Smarty; Afterwards, it is recommended to print echo $tpl->template_dir;
to see if your index.htm is in this directory