smarty-php / smarty-php/smarty

nocache in cachable plugins

Open
#929 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
2.3k
Forks
709
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied PHP reproduction, especially display() and the registered testFunc that calls fetch('string:...'). Trace why nocache tags and variable assignments behave differently inside the plugin-rendered template. Done means the plugin output preserves the expected dynamic values, matching the direct display() behavior, with coverage for the reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.