smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.24k stars 705 forks source link

nocache in cachable plugins #929

Open kkFelix opened 7 months ago

kkFelix commented 7 months ago

Is there any way to use nocache tags or other methods to display uncached content within a cachable plugin? This is my test code:

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
require __DIR__ . '/vendor/autoload.php';
$smarty = new Smarty();
$smarty->setCacheDir(__DIR__ . '/cache');
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
$smarty->setCompileDir(__DIR__ . '/compile');
$smarty->assign('dyn', microtime(true), true);
$smarty->assign('stat1', microtime(true));
$smarty->assign('stat2', microtime(true));
$smarty->registerPlugin(
    Smarty::PLUGIN_FUNCTION,
    'testFunc',
    static function ($params, $smarty) {
        return $smarty->fetch('string:test@plugin: {$dyn}, {$stat1}, {nocache}{$stat2}{/nocache}');
    },
    true
);
$smarty->display('string:test@index: {$dyn}, {$stat1}, {nocache}{$stat2}{/nocache}<br>{testFunc}');

The output from the display() call behaves correctly: the $dyn variable stays dynamic since it was assigned with cachable=false, $stat1 stays static and $stat2 within the nocache-tags also is dynamic.

Within the plugin however, all 3 variables are always cached.

test@index: 1706176477.52, 1706176474.2449, 1706176477.52
test@plugin: 1706176474.2449, 1706176474.2449, 1706176474.2449