Home  >  Article  >  Backend Development  >  More complete notes on PHP’s Smarty

More complete notes on PHP’s Smarty

不言
不言Original
2018-04-19 15:10:079698browse

The content introduced in this article is a relatively complete note about PHP Smarty, which has a certain reference value. Now I share it with you. Friends in need can refer to it

1 , Set directory file:



## 2. Configuration file configuration information:

This should be the content in index.php:




[php] view plain copy



    <?php  
        header("content-type:text/html;charset=utf8");  
        include_once("libs/Smarty.class.php"); //包含smarty类文件   
      
        $smarty = new Smarty(); //建立smarty实例对象$smarty   
        $smarty->compile_dir = &#39;./templates_c/&#39;; //设置模板目录 ——这里的文件很重要的,需要写的模板文件  
        $smarty->compile_dir = &#39;./templates_c/&#39;;; //设置编译目录 ——混编文件,自动生成  
        $smarty->cache_dir = &#39;./cache/&#39;; //缓存目录   
        $smarty->cache_lifetime = 0; //缓存时间   
        $smarty->caching = true; //缓存方式   
      
        $smarty->left_delimiter = "{";   
        $smarty->right_delimiter = "}";   
      
        $smarty->assign("name", "注释"); //进行模板变量替换   
        $smarty->display("index.html"); //编译并显示位于./templates下的index.htm模板   
    ?>


##Explanation of some routines, but it is necessary to understand:

2.1: include_once statement:

It will be installed to the website The smarty file is included into the current file. Note that the included path must be written correctly.

2.2:$smarty = new Smarty():

This sentence creates a new Smarty object $smarty, a simple instantiation of an object.

2.3:$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. Actually, when writing When programming, we need to write this sentence clearly. This is also a good programming style.

2.4:$smarty->templates_c(""):

##This One 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 in Linux On the server, please ensure that the directory defined in teamplates_c has writable and readable permissions. By default, its directory is The translation directory is templates_c in the current directory. For the same reason, we will write it out clearly.

##2.5: $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 want to use 3f1c4e4b6b16bbbd69b2ee476dc4f83a in the template, the definition of the function in Script will inevitably be

Use {}, although it has its own solution, but it is customary for us to redefine it as "{#" and "#}" or "438ccb24c2dcc8dfa8cf6c7f24445db2" or other identifiers, note, if left and right

## are defined here After the # separator, each variable in the template file must use the same symbol as the definition, for example, specified here as "5605d57dd60943219d1ec82ee5982947 ", the htm template should also change {$name} accordingly

# to f1ae60a094afb5383c24c6d7d49bae7e, like this Only then can the program find the template variables correctly.

2.6:$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

is the cache directory in the current directory, which is equivalent to the templates_c directory. In #linuxWe need to ensure its readability and writability in the system.

2.7:$smarty->cache_lifetime = 60 * 60 * 24:

# #Here, 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, and when it is 0, it means that the cache is always deleted every time the program is executed. Re-establish. The above setting means setting cache_lifetime to one day.

2.8:$smarty->caching = 1:

This attribute tells Whether Smarty wants to cache and how to cache. It can take 3 values, 0: Smarty default value, which means that the template will not be cached; 1: means that Smarty will

use the currently defined cache_lifetime to decide whether End cache; 2: Indicates that 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.

2.9:$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 name of the variable to be replaced by the template variable ; The second original form is

assign(mixed var). We will explain this member function in detail in the following examples. How to use, assign is one of Smarty’s core functions, all substitutions for template variables

# Use it if you change it.

2.10:$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. The template file here does not need to add a path , just use a file name. , whose path we have already defined in $smarty->templates(string path).

After the program is executed, we can open the templates_c and cache directories in the current directory, and we will find that there are some more %% directories below. These directories are

Smarty compilation and cache directory are automatically generated by the program. Do not modify these generated files directly.


##3. Smarty’s note:

The boring text part can be ignored: Template comments are surrounded by * asterisks, and the asterisks on both sides are surrounded by delimiters, such as {* this is a comment *}. Smarty comments will not appear in the final output of the template file, which is different from f99bb4960eac893e67798271fc50e3bb (Annotation: HTML comments are visible in the page source code, but smarty comments are not). This is very useful. Imagine that the comments only exist in the template and no one can see them in the output page :).

Feel it intuitively:



##4. Constants:

The boring text part can be ignored: The configuration file variables are One does not use the dollar sign $, but uses # signs surrounding the variable (#hashmarks#), or a variable in the form of $smarty.config.

Feel it intuitively:



##5. Variables :

The boring text part can be ignored: Template variables start with the dollar sign $ and can contain numbers, letters and underlines, which is the same as php variables Very similar. You can reference numeric or non-numeric indexes into arrays, as well as object properties and methods.


## Translation Note: Follow the instructions like $abc, $abc123, $abc_123, $abc[ 1], $abc['a'], $abc->a, $abc->a() are all valid template variables.


Feel it intuitively:


Before the variable is output, it must be There must be a $ sign! ! ! ! ! ! ! ! ! ! !


6. Function



7. Assignment array



##8. Variable regulator (the first variable is in front of the vertical bar, the second and third variables are after :)..... ...variable)




9. Conditional judgment




##10. Section loop







11、foreach




#12. The introduction of the file puts an object of a class in the form of a variable


##13. Use assign to assign values ​​to the smarty template:


14. Using PHP function, refer to the variable regulator in Article 8



## 15. Use of custom functions



##16. functions Definition and use of plug-ins

##17, modifier variable regulator Definition and use

18. Definition and use of block function plug-in use

19, for loop and while loop examples



##20. Use of img and option tags

##21. Use of {block}

Related recommendations:

smarty template engine configuration file data and retained data

Detailed explanation of comments and truncation functions in Smarty

Detailed explanation of Smarty built-in function include in PHP

The above is the detailed content of More complete notes on PHP’s Smarty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn