Smarty note, smarty

Source: Internet
Author: User

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.

 




 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.