I have been using other template engines all the time. When I was attending bkJia training courses today, I spoke about the partial cache of the smarty template engine. I feel pretty good. I'm so impressed with my understanding here, if something is wrong, I hope my friend will reply with me. We will encourage you to learn from each other. At the same time, I would like to thank Mr. Zhang for providing a very good learning platform for PHP beginners like ours.
I found that smarty is so powerful and magical that I like it as much as the template engine I used previously.
Because I enable the smarty cache by default, but in some places the data is updated in real time or updated quickly, it is not suitable for caching, so that local cache will be useful.
1. insert Method
Defines the display time of a function:
- function insert_get_current_time(){
- $timestamp=emptyempty($timestamp)?time():$timestamp;
- $timeoffset=(int) +8;
- return $ret=gmdate("Y-n-j g:ia", $timestamp + $timeoffset * 3600);
- }
Then in the template:
- {insert name="get_current_time"}
In this way, each time the page is opened, the displayed time is real-time, not cached. Note that the function name must start with insert, and the name in the template corresponds to it.
This method is simple, but it is not suitable if the content to be displayed is large.
2. Dynamic block Method
- // Partial Cache
- Function smarty_block_nocache ($ param, $ content, $ smarty)
- {
- Return $ content;
- }
- $ Smarty-> register_block (nocache, smarty_block_nocache, false );
In the template:
- {nocache}
- {$smarty.now}
- {/nocache}
In this way, the time displayed for each page refresh is different.
3. Plug-in block Method
Create an object in the Smarty/plugins directory
The content of block. nocache. php is as follows:
- <?php
- function smarty_block_nocache($param, $content, $smarty)
- {
- return $content;
- }
- ?>
This is the same as method 2, and the labels in the template are the same. Register_block is unnecessary in the PHP file, which is very convenient.