Home > Backend Development > PHP Tutorial > More complete notes on PHP's Smarty

More complete notes on PHP's Smarty

不言
Release: 2023-03-24 12:28:01
Original
9783 people have browsed it

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 = './templates_c/'; //设置模板目录 ——这里的文件很重要的,需要写的模板文件  
        $smarty->compile_dir = './templates_c/';; //设置编译目录 ——混编文件,自动生成  
        $smarty->cache_dir = './cache/'; //缓存目录   
        $smarty->cache_lifetime = 0; //缓存时间   
        $smarty->caching = true; //缓存方式   
      
        $smarty->left_delimiter = "{";   
        $smarty->right_delimiter = "}";   
      
        $smarty->assign("name", "注释"); //进行模板变量替换   
        $smarty->display("index.html"); //编译并显示位于./templates下的index.htm模板   
    ?>
    Copy after login


##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

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template