smarty-php / smarty

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

Error: For loop not working when using extends resource type #1036

Open Synaptic14 opened 5 months ago

Synaptic14 commented 5 months ago

When extending a template using the extends resource type, for loops throw "Error: Attempt to assign property "step" on null"

PHP Version: 8.3.6 Smarty Version: 5.3.1

index.php

require_once('vendor/autoload.php');
use Smarty\Smarty;

$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__.'/tpl/');
$smarty->setCompileDir(__DIR__.'/tpc/');

try {
    $smarty->display('extends:parent.tpl|child.tpl');
} catch (Exception $e) {
    echo $e->getMessage();
}

parent.tpl

<!doctype html>
<html lang="en">
<head>
    <title>Smarty Error Example</title>
</head>
<body>
    {block name="content"}{/block}
</body>
</html>

child.tpl

{block name="content"}
    <h1>For Loop</h1>
    <ul>
        {for $i=1 to 10}
            <li>{$i}</li>
        {/for}
    </ul>
{/block}
monkeylogic commented 3 months ago

Is there any update on this? We really need a fix.

antman3351 commented 1 month ago

Just open a PR with a fix. Until then as temporary workaround if you've registered the function range() you can use: {foreach range( 0, $len ) as $i}...{/foreach}