smarty
smarty copied to clipboard
Error: For loop not working when using extends resource type
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}
Is there any update on this? We really need a fix.
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}
As workaround I've changed:
{for $panel=3 to $welcome_panels}
<div id="panel-{$panel}">
</div>
{/foreach}
to:
{section name=panel start=3 loop=$welcome_panels+1}
<div id="panel-{$smarty.section.panel.index}">
</div>
{/section}