Home >Backend Development >PHP Tutorial >Introduction to several methods of Smarty partial caching_PHP tutorial
Many times when we use smarty, we do not want to cache the entire page, such as weather, stocks and other information that is updated in real time. Then we can make it not cached. Common implementation methods include the following three methods:
1.insert method:
The Insert function is similar to the inluce function, except that the content contained in insert will not be cached, and the function will be re-executed every time the template is called. For example, let’s define a function that displays time:
In this way, every time you open the page, the real-time time will be displayed instead of the cached time. Note that the function name here must start with insert, and the name in the template corresponds to it. If our function contains some parameters, then our template can be like this:
In smarty code:
In template file:
3. Plug-in block method:
This is similar to block, but we do it in the form of a plug-in. Create a file in the Smarty/plugins directory: block.nocache.php. The naming here must be standardized, otherwise smarty will not recognize it. The content is as follows:
In the template and above, just add nocache to places that do not need to be cached!