Smarty creates cache for includes with cache_id even when it's null and cache disabled
I have Smarty cache globally disabled, cache_lifetime set to 0, but include with any cache_id still creates cache files.
{include file='example.tpl' cache_id=NULL}
Any way to disable this behavior?
v5.4.1
Hello,
- To disable caching for a specific code snippet or include file, wrap the include tag or code snippet with
{nocache}{/nocache}and remove thecache_idattribute from the include tag.
Example: {nocache}{include file='example.tpl'}{/nocache}
But I need to cache it when cache is enabled or cache_id is not null.
Then you should use a statement behaviour {if} {/else} like {if $cache_enabled} {include file='example.tpl'} {else} {nocache} {include file='example.tpl'} {/nocache} {/if}
I thought about it, but it's too verbose. The main problem is with cache_id - if set to NULL in PHP, Smarty stops caching, if set to NULL in the Smarty template, it still caches. And it shouldn't ignore the fact that caching is actually disabled.
It would be better if you provide the proper code snippet that addresses your issue, so I can help you more effectively. We can also optimize the code by understanding its functionality once you provide it
- Normally, cache is enabled globally and Smarty is called with
Smarty->fetch($template, $cache_id) cache_idis generated with special function:
public function generateCacheKey(string|null $prefix = null, array|null $options = null): string|null
{
if (!$this->design->cachingEnabled) {
return null;
}
$defaultOptions = [
'useHost' => true,
'usePath' => true,
'useQuery' => true,
'cachedQueryParams' => ['page'],
'ignoredQueryParams' => []
];
...
- Same function is used in .tpl for parts that require different caching policy:
{include file='components/page--catalog/footer/brands-and-categories.tpl' cache_id=generateCacheKey('brands-and-categories', ['useHost' => false, 'useQuery' => false])}
Works fine usually, but in .tpl Smarty still creates cache files even when function returns null and when caching is disabled in Smarty... I think this is a bug.
UPD... Actually, ->fetch is used via custom function that has if ($cache_id === null) { $this->smarty->setCaching(0); }. That explains the difference in behavior, but doesn't solve the issue.
Hey,
Sorry for taking so long. This could be a bug. But here is a solution for you :
- Edit : /smarty/smarty/src/Compile/Tag/IncludeTag.php
- Line 137 change from
if (isset($_attr['cache_id']){toif (isset($_attr['cache_id']) && $_attr['cache_id'] != 'null') {
This is because assigning the cache_id seems to be set as a string and not as a null type
Could you please let me know if it works or not?
Huh, turns out, it's not that simple... Tried printing $_attr['cache_id'], it is not processed at this point:
$_smarty_tpl->getSmarty()->getModifierCallback('generateCacheKey')('head-analytics--google',array('usePath'=>false,'useQuery'=>false))
Verbose syntax it is, I guess...
Huh, turns out, it's not that simple... Tried printing
$_attr['cache_id'], it is not processed at this point:$_smarty_tpl->getSmarty()->getModifierCallback('generateCacheKey')('head-analytics--google',array('usePath'=>false,'useQuery'=>false))
Mmm🤔
Well, $_smarty_tpl->getSmarty()->getModifierCallback('generateCacheKey')('head-analytics--google', array('usePath' => false, 'useQuery' => false)) is not directly related to the include tag class that I asked you to modify and is only processed if the .tpl file contains any include tags.
I thought about it, but it's too verbose. The main problem is with
cache_id- if set to NULL in PHP, Smarty stops caching, if set to NULL in the Smarty template, it still caches. And it shouldn't ignore the fact that caching is actually disabled.
Have you tried passing the value for cache_id from a template variable (populated from PHP) instead of literally using NULL in the template? NULL is not a reserved word in Smarty in the way it is in PHP. It is interpreted as a string 'NULL'.
I thought about it, but it's too verbose. The main problem is with
cache_id- if set to NULL in PHP, Smarty stops caching, if set to NULL in the Smarty template, it still caches. And it shouldn't ignore the fact that caching is actually disabled.Have you tried passing the value for
cache_idfrom a template variable (populated from PHP) instead of literally using NULL in the template? NULL is not a reserved word in Smarty in the way it is in PHP. It is interpreted as a string'NULL'.
I created a structure identical to the code snippet he provided, keeping if (isset($_attr['cache_id']) && strtolower($_attr['cache_id']) != 'null') {, so cache_id works even when it's assigned the string 'NULL', and it works fine for me.
I created a structure identical to the code snippet he provided, keeping
if (isset($_attr['cache_id']) && strtolower($_attr['cache_id']) != 'null') {, so cache_id works even when it's assigned the string 'NULL', and it works fine for me.
I created a structure identical to the code snippet he provided, keeping
if (isset($_attr['cache_id']) && strtolower($_attr['cache_id']) != 'null') {, so cache_id works even when it's assigned the string 'NULL', and it works fine for me.
No, $_smarty_tpl->getSmarty()->getModifierCallback(...) doesn't affect the cache_id value. What if you consider assigning a value in PHP and setting it as the cache_id instead of calling the function?
No,
$_smarty_tpl->getSmarty()->getModifierCallback(...)doesn't affect thecache_idvalue. What if you consider assigning a value in PHP and setting it as the cache_id instead of calling the function?
But then cache_id will be $_smarty_tpl->getValue('cache_id'), not calculated value.
That's not really relevant. The more important issue is that with any cache_id present, Smarty will create a cache, even though caching is disabled by default in init.
Then we should try adding another statement to check whether the cache is disabled by default in the initialization or not, and proceed accordingly.