I have been using other template engines before. Today, when I was listening to the bkJia training course, I talked about the local cache of the smarty template engine. It felt very good. I combined my own understanding here and sighed. If there is anything wrong, place, I hope friends will reply. Let’s all learn together. At the same time, I would also like to thank Teacher Zhang for providing a very excellent learning platform for PHP beginners like us.
I discovered that smarty is such a powerful and magical thing, which makes me like it as much as the template engine I used before.
Because I enable smarty’s cache by default, but the data in some places is updated in real time or updates quickly, so it is not suitable for caching. In this way, local caching will be useful.
1. insert method
Define a function to display time:
<ol class="dp-c"> <li class="alt"><span><span class="keyword">function</span><span> insert_get_current_time(){ </span></span></li> <li> <span> </span><span class="vars">$timestamp</span><span>=</span><span class="func">empty</span><span class="keyword">empty</span><span>(</span><span class="vars">$timestamp</span><span>)?time():</span><span class="vars">$timestamp</span><span>; </span> </li> <li class="alt"> <span> </span><span class="vars">$timeoffset</span><span>=(int) </span><span class="string">+8</span><span>; </span> </li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$ret</span><span>=</span><span class="func">gmdate</span><span>(</span><span class="string">"Y-n-j g:ia"</span><span>, </span><span class="vars">$timestamp</span><span> + </span><span class="vars">$timeoffset</span><span> * 3600); </span> </li> <li class="alt"><span>} </span></li> </ol>
Then in the template:
<ol class="dp-c"><li class="alt"><span><span>{insert name=</span><span class="string">"get_current_time"</span><span>} </span></span></li></ol>
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.
This method is simple, but if the content to be displayed is a large piece, it should not be used.
2. Dynamic block method
<ol class="dp-c"> <li class="alt"><span><span class="comment">//部分缓存 </span><span> </span></span></li> <li> <span class="keyword">function</span><span> smarty_block_nocache(</span><span class="vars">$param</span><span>, </span><span class="vars">$content</span><span>, </span><span class="vars">$smarty</span><span>) </span> </li> <li class="alt"><span> { </span></li> <li> <span> </span><span class="keyword">return</span><span> </span><span class="vars">$content</span><span>; </span> </li> <li class="alt"><span>} </span></li> <li> <span class="vars">$smarty</span><span>->register_block(</span><span class="string">nocache</span><span>, </span><span class="string">smarty_block_nocache</span><span>, false); </span> </li> </ol>
In template:
<ol class="dp-c"> <li class="alt"><span><span>{nocache} </span></span></li> <li> <span>{</span><span class="vars">$smarty</span><span>.now} </span> </li> <li class="alt"><span>{/nocache} </span></li> </ol>
In this way, every time you refresh the page, the displayed time will be different.
3. Plug-in block method
Create a file in the Smarty/plugins directory
Block.nocache.php content is as follows:
<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span class="keyword">function</span><span> smarty_block_nocache(</span><span class="vars">$param</span><span>, </span><span class="vars">$content</span><span>, </span><span class="vars">$smarty</span><span>) </span></li><li class="alt"><span>{ </span></li><li><span> </span><span class="keyword">return</span><span> </span><span class="vars">$content</span><span>; </span></li><li class="alt"><span>} </span></li><li><span>?> </span></span></li></ol>
This has the same effect as method 2, and the tags in the template are also the same. There is no need to register_block in the php file, which is very convenient.