Smarty note, smarty
Smarty is often used in projects, but it is always used in simple assgin display foreach if else and other methods. I read smarty again. Simple recording of useful things.
Smarty system reserved variable $ smarty. get # usage $ smarty. get. id and so on $ smarty. post $ smarty. session $ smarty. cookie $ smarty. server
Use constants defined in PHP in the smarty Template
Define ('hieght ', 8846) in the php file );
Use $ smarty. const. HIEGHT in the template file
Smarty reads the variables in the configuration file
1 $ smarty-> config_dir = "./conf"; # set the path of the configuration file
2 configuration file writing rules 1) configuration file ending with. conf 2) configuration file writing rules key1 = val1 key1 = val1
3 usage: in the template file header {config_load file = 'site. conf '}
4 {$ smarty. config. site} To get // {# site #} can also be read
Smarty cache settings
$ Smarty-> caching = true; # enable cache page
$ Smarty-> cache_lifetime = 3600; # Cache Time
$ Smarty-> cache_dir = './cache'; # directory for storing files for change
$ Smarty-> isCached('news.html ') can be used to determine whether a cached file is generated # In smarty3
# Example of generating multiple cache files on one page: A product details page displays different content based on the product ID
$ Id = empty ($ _ GET ['id])? 1: $ _ GET ['id'];
$ Good_name = 'good _ name _ '. $ id;
$ Smarty-> assign ('good _ name', $ good_name );
$ Smarty-> display('tpm.html ', $ id); # different pages are generated based on the id.
Note: In actual projects, this ID should be calculated based on parameters. For example, calculated based on parameters such as page and category. Previously, a company project used the smarty cache. The value of this id is obtained after page category md5. The same is true in ecshop, but not MD5.
# Clear Cache
$ Smarty-> using AchE ('tmp _ name ')
$ Smarty-> clearAllCache ()
# Set local cache-free methods
1 In the tag {$ time | date_format: "% Y-% M-% D % H: % M: % S" nocache}
2 {nocache} {/nocache} controls local non-Cache
3 when assigning values to php, set the third parameter that does not cache assign to true.
4. An insert method in smarty doesn't seem useless)
###### I personally feel that smarty's cache is of little significance. Because it can be replaced by redis MEMCHACHE. Of course, the existence is reasonable. Haha
# Variable regulator: Simply put, the function of modifying the display form of variables is as follows:
{$ Time | date_format: "% Y-% M-% D % H: % M: % S" nocache}
# You can also write the variable regulator yourself by referring to the file in the plugin. ### it feels useful. It is used in our project. For specific use, go back and make up.
Get smarty:
1 download: http://www.smarty.net/download
2. Get it through composer: composer is a good thing.