Smarty quick start getting started in half an hour. Smarty's quick start guide in half an hour. smarty's quick start guide describes how to get started with smarty, allowing readers to quickly learn how to use it within half an hour. Shared with big smarty, Quick Start Guide for half an hour, and quick start for smarty
This article describes how to get started with smarty, allowing readers to quickly learn how to use it within half an hour. Share it with you for your reference. The specific implementation method is as follows:
I. smarty program design:
In the template design section of smarty, I briefly introduced some common settings of smarty in the template. this section mainly introduces How to Start program design in smarty. Download the Smarty file and place it on your site.
The index. php code is as follows:
The code is as follows:
<? Php
/**
*
* @ Version $ Id: index. php
* @ Package
* @ Author www.jb51.net
* @ Action: Display the instance program
*/
Include_once ("./Smarty. class. php"); // contains the smarty class file
$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> templates ("./templates"); // you can specify the Template directory.
$ Smarty-> templates_c ("./templates_c"); // you can specify the compiling directory.
$ Smarty-> cache ("./cache"); // cache Directory
$ Smarty-> cache_lifetime = 0; // cache time
$ Smarty-> caching = true; // cache 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 under./templates
?>
2. explain the smarty program
As we can see, the smarty program is actually a set of code that complies with the php language specification. let's explain it one by one:
1:/**/statement:
The included part is the program header annotation. The main content should be a brief introduction to the role of the program, copyright and the author and the writing time, which is not necessary in the smarty, but in terms of the program style, this is a good style.
2: include_once statement:
It includes the smarty file installed on the website to the current file. Note that the included path must be correctly written.
3: $ smarty = new Smarty ():
This statement creates a Smarty object $ smarty, which is a simple object instantiation.
4: $ smarty-> templates (""):
This statement specifies the path for the $ smarty object to use the tpl template. it is a directory. if this statement is not provided, the default template path of Smarty is the templates Directory of the current directory, when writing a program, we need to write this sentence, which is also a good program style.
5: $ smarty-> templates_c (""):
This statement specifies the directory when the $ smarty object is compiled. In the template design article, we already know that Smarty is a compilation template language, and this directory is the directory for compiling the template. Note that if the site is on a linux server, make sure that
This directory defined in teamplates_c has writable and readable permissions. by default, its compiling directory is templates_c under the current directory. for the same reason, we will write it clearly.
6: $ smarty-> left_delimiter and $ smarty-> right_delimiter:
Specifies the left and right delimiters when searching for template variables. The default values are "{" and "}", but actually, because we want to use "script" in the template, the function definition in the Script will inevitably use {}, although it has its own solution, we are used to redefining it.
For "{#" and "#}" or" "Or other flags. note: If the left and right delimiters are defined here, each variable must use the same symbol as the definition in the template file, for example, if you specify "<{" and "}>" here, the htm template also needs
Change {$ name} to <{$ name}> so that the program can find the template variables correctly.
7: $ smarty-> cache ("./cache "):
Tell the template file cache location output by Smarty. In the previous article, we learned that the biggest advantage of Smarty is that it can be cached. here, we set the cache directory. By default, it is the cache directory under the current directory, which is equivalent to the templates_c directory.
Make sure it is readable and writable.
8: $ smarty-> cache_lifetime = 60*60*24:
Here, the effective cache time is calculated in seconds. When the first cache time expires, when the caching variable of Smarty is set to true, the cache will be rebuilt. When the value is-1, it indicates that the created cache never expires. if it is 0, it indicates that the cache is slow every time the program is executed.
The storage is always re-created. The preceding settings indicate that cache_lifetime is set to one day.
9: $ smarty-> caching = 1:
This attribute tells Smarty whether to cache and how to cache it. It can take three values, 0: Smarty default value, indicating that the template is not cached; 1: indicates that Smarty will use the current defined cache_lifetime to determine whether to end the cache; 2: Indicates
Smarty uses the cache_lifetime value when the cache is created. Traditionally, true and false are used to indicate whether the cache is performed.
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, and var indicates the variable name to replace the template variable; the second prototype is assign (mixed var). We will explain in detail how to use this member function in the following example. assign is one of the core functions of Smarty, all replace Template variables must use it.
11: $ smarty-> display ("index. tpl "):
Display (string varname) is used to display a template. To put it simply, it displays the templates that have been analyzed and processed. the template file here does not need to be added with a path. you only need to use a file name, the path is defined in $ smarty-> templates (string path.
After the program is executed, we can open the templates_c and cache directories under the current directory, and we will find that there are more % directories below, which are the compilation and cache directories of Smarty, it is automatically generated by the program. do not directly modify these generated files.
I briefly introduced some common basic elements in the Smarty program. in the following example, you can see that they will be used multiple times.
III. Template description
Next we will introduce a section loop block and a foreach loop block. Originally, it should belong to the template part, but because they are the essence of smarty and closely related to the smarty program design part, so let's talk about it separately in this section.
1: foreach: Used to loop a simple array. it is a selective section loop. Its Definition format is:
The code is as follows:
{Foreach from = $ array item = array_id}
{Foreachelse}
{/Foreach}
Here, from indicates the array variable to be cyclic. item is the name of the variable to be cyclic, and the number of loops is determined by the number of array variables specified by from. {Foreachelse} is used for processing when the passed array in the program is empty. The following is a simple example:
Template File:
The example.htm page is as follows:
The code is as follows:
Foreach outputs a "two-dimensional join array" data:
{# Foreach item = new from = $ news #}
News No.: {#$ new. id #}
News content: {#$ new. title #}
{# Foreachelse #}
No news output in the database!
{#/Foreach #}
{Foreach from = $ newsArray item = newsID}
News No.: {$ newsID}
News: {$ newsTitle}
{Foreachelse}
Sorry, there is no news output in the database!
{/Foreach}
This is an error that does not display data. This article has corrected it.
Program File: example. php:
The code is as follows:
<? Php
/*************************************** ******
*
* File name: example. php
* For use: instance program 2 is displayed.
**************************************** *****/
Include_once ("./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" => "1st News ");
$ Array [] = array ("newsID" => 2, "newsTitle" => "2nd news ");
$ Array [] = array ("newsID" => 3, "newsTitle" => "3rd News ");
$ Array [] = array ("newsID" => 4, "newsTitle" => "4th news ");
$ Array [] = array ("newsID" => 5, "newsTitle" => "5th News ");
$ Array [] = array ("newsID" => 6, "newsTitle" => "6th news ");
// This is a two-dimensional joined array
$ Smarty-> assign ("newsArray", $ array );
// Compile and display the index.htm template under./templates
$ Smarty-> display ("example.htm ");
?>
Output result: example. php:
The code is as follows:
Here an array is output:
News No.: 1
News: 1st News
News No.: 2
News: 2nd news
News No.: 3
News: 3rd News
News No.: 4
News: 4th News
News No.: 5
News: 5th News
News No.: 6
News: 6th news
Foreach can also use foreachelse to match and use foreachelse to indicate the operations to be performed by the program when the array passed to foreach is null. for specific usage instructions, see the manual description.
2. section:
The section is designed to solve the problem of foreach. like foreach, it is used to design the loop blocks in the template. it is complex and can meet the needs of the program greatly, so in the program, I am used to using it instead of using foreach. The basic prototype is:
The code is as follows:
{Section name = name loop = $ varName [, start = $ start, step = $ step, max = $ max, show = true]}
The parameters are described as follows:
Name: section name, no need to add $
$ Loop: the variable to be recycled. in the program, use assign to operate the variable.
$ Start: subscript of the start loop. the subscript of the loop starts from 0 by default.
$ Step: increment of the mark in each loop
$ Max: maximum loop subscript
$ Show: boolean type. determines whether to display this block. the default value is true.
Here is a glossary:
Loop subscript: Actually, its English name is index, which means index. here I translate it into "subscript" for better understanding. It indicates the current loop index when the loop block is displayed. the default value starts from 0 and is affected by $ start. if $ start is set to 5, it will also count from 5, we used it in the template design section. this is an attribute of the current {section} and the call method is Smarty. section. sectionName. index. here, sectionName refers to the name attribute in the function prototype.
The attribute values of the {section} block are:
1. index: the "loop subscript" introduced above. the default value is 0.
2. index_prev: The first value of the current underlying object. the default value is-1.
3. index_next: the next value of the current underlying object. the default value is 1.
4. first: whether it is the first next loop
5. last: whether it is the last loop
6. iteration: number of cycles
7. rownum: current row number, another alias of iteration
8. loop: the last cycle number, which can be used to count the number of cycles of a section after the section block.
9. total: number of cycles. the number of cycles can be counted after the section.
10. show: it is included in the function declaration to determine whether the section is displayed.
For details about their attributes, refer to the manual. you can use these attributes flexibly in the program. I have used the index attribute in the template section.
Similarly, {section} can be used with {sectionelse} to process the template when the input array variable is null.
We use {section} in the above example to replace {foreach} to implement the sample function. note, in this example, I only implement {foreach} in the tpl template using {section}. The php program file is not modified, and the {sectionelse} processing block is added:
The example. tpl template file is as follows:
The code is as follows:
Here an array is output:
{Section name = loop = $ News}
News No.: {$ News [loop]. newsID}
News Title: {$ News [loop]. newsTitle}
{Sectionelse}
Sorry, there is no news input!
{/Section}
The example. php file is as follows:
The code is as follows:
<? Php
/*************************************** ******
*
* File name: example7.php
* For use: instance program 2 is displayed.
**************************************** *****/
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" => "1st News ");
$ Array [] = array ("newsID" => 2, "newsTitle" => "2nd news ");
$ Array [] = array ("newsID" => 3, "newsTitle" => "3rd News ");
$ Array [] = array ("newsID" => 4, "newsTitle" => "4th news ");
$ Array [] = array ("newsID" => 5, "newsTitle" => "5th News ");
$ Array [] = array ("newsID" => 6, "newsTitle" => "6th news ");
$ Smarty-> assign ("newsArray", $ array );
// Compile and display the index. tpl template under./templates
$ Smarty-> display ("example. tpl ");
?>
The output file example. php is as follows:
The code is as follows:
Here an array is output:
News No.: 1
News: 1st News
News No.: 2
News: 2nd news
News No.: 3
News: 3rd News
News No.: 4
News: 4th News
News No.: 5
News: 5th News
News No.: 6
News: 6th news
Here, the {section} block feels awkward about the variable naming method, but it doesn't matter. you just need to remember the template variable usage:
$ LoopName [name]. in var mode, loopName is the variable name assigned to the loop, and [name] is the string assigned to the name ,. then you need to set the subscript name corresponding to the value in the program array.
Well, this article's guide to smarty program design is written here. for general applications, this knowledge is enough. for other advanced skills, please refer to the examples in this manual, in addition, there are examples of Smarty in practical applications, such as mysql statements built in php, DB classes in phplib, ADODB, and Pear. If you are interested, take a look at the relevant content.
I hope this article will help you with PHP programming.
An error occurred while learning PHP template engine Smarty: C: \ AppServ \ www \ mb \ smarty \ Smartyclassphp on line 780.
First, set the three categories for recruitment
$ Smarty-> template_dir = './templates /';
$ Smarty-> compile_dir = './templates_c /';
$ Smarty-> cache_dir = './cache/'; and ensure that these three directories exist and the path is correct.
Second, your template is incorrectly written.
Should be changed
News No.: {$ newsID. newsID}
News: {$ newsID. newsTitle}
During development, it is best not to enable smarty cache to convert $ smarty-> caching = false;
Error message: Smartyclassphp on line 1095
Cannot "read" to retrieve resource index.htm
The smarty read template action failed for three reasons
1.index.htm has no read permission. if you are a newbie, you should exclude this reason in win.
2. no template file exists, and index.htm does not exist. as you know, you must have created such a template.
3. the template path is incorrect. this is your crux.
After $ tpl = new Smarty;, we recommend that you print echo $ tpl-> template_dir;
Check that your index.htm is not in this directory.
This article describes how to get started with smarty, allowing readers to quickly learn how to use it within half an hour. Share with me...