smarty icon indicating copy to clipboard operation
smarty copied to clipboard

Error: For loop not working when using extends resource type

Open Synaptic14 opened this issue 1 year ago • 3 comments

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}

Synaptic14 avatar Jun 20 '24 02:06 Synaptic14

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

monkeylogic avatar Jul 26 '24 16:07 monkeylogic

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}

antman3351 avatar Oct 08 '24 07:10 antman3351

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}

chilek avatar Nov 26 '25 00:11 chilek