Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1036.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed "Attempt to assign property step on null" error when using a {for} loop inside a block of an extended template [#1036](https://github.com/smarty-php/smarty/issues/1036)
27 changes: 15 additions & 12 deletions src/Compile/Tag/ForTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
$var = $_statement['var'];
$index = '';
}
$output .= "\$_smarty_tpl->assign($var, null);\n";
$output .= "\$_smarty_tpl->tpl_vars[$var]->value{$index} = {$_statement['value']};\n";
$itemVar = "\$_smarty_tpl->getVariable({$var})";
$output .= "\$_smarty_tpl->assign($var, []);\n";
$output .= "{$itemVar}->value{$index} = {$_statement['value']};\n";
}
if (is_array($_attr['var'])) {
$var = $_attr['var']['var'];
Expand All @@ -61,7 +62,8 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
$var = $_attr['var'];
$index = '';
}
$output .= "if ($_attr[ifexp]) {\nfor (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n";
$itemVar = "\$_smarty_tpl->getVariable({$var})";
$output .= "if ($_attr[ifexp]) {\nfor (\$_foo=true;$_attr[ifexp]; {$itemVar}->value{$index}$_attr[step]) {\n";
} else {
$_statement = $_attr['start'];
if (is_array($_statement['var'])) {
Expand All @@ -71,21 +73,22 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
$var = $_statement['var'];
$index = '';
}
$output .= "\$_smarty_tpl->assign($var, null);";
$itemVar = "\$_smarty_tpl->getVariable({$var})";
$output .= "\$_smarty_tpl->assign($var, []);";
if (isset($_attr['step'])) {
$output .= "\$_smarty_tpl->tpl_vars[$var]->step = $_attr[step];";
$output .= "{$itemVar}->step = $_attr[step];";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$var]->step = 1;";
$output .= "{$itemVar}->step = 1;";
}
if (isset($_attr['max'])) {
$output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) min(ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step)),$_attr[max]);\n";
$output .= "{$itemVar}->total = (int) min(ceil(({$itemVar}->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs({$itemVar}->step)),$_attr[max]);\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step));\n";
$output .= "{$itemVar}->total = (int) ceil(({$itemVar}->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs({$itemVar}->step));\n";
}
$output .= "if (\$_smarty_tpl->tpl_vars[$var]->total > 0) {\n";
$output .= "for (\$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value], \$_smarty_tpl->tpl_vars[$var]->iteration = 1;\$_smarty_tpl->tpl_vars[$var]->iteration <= \$_smarty_tpl->tpl_vars[$var]->total;\$_smarty_tpl->tpl_vars[$var]->value{$index} += \$_smarty_tpl->tpl_vars[$var]->step, \$_smarty_tpl->tpl_vars[$var]->iteration++) {\n";
$output .= "\$_smarty_tpl->tpl_vars[$var]->first = \$_smarty_tpl->tpl_vars[$var]->iteration === 1;";
$output .= "\$_smarty_tpl->tpl_vars[$var]->last = \$_smarty_tpl->tpl_vars[$var]->iteration === \$_smarty_tpl->tpl_vars[$var]->total;";
$output .= "if ({$itemVar}->total > 0) {\n";
$output .= "for ({$itemVar}->value{$index} = $_statement[value], {$itemVar}->iteration = 1;{$itemVar}->iteration <= {$itemVar}->total;{$itemVar}->value{$index} += {$itemVar}->step, {$itemVar}->iteration++) {\n";
$output .= "{$itemVar}->first = {$itemVar}->iteration === 1;";
$output .= "{$itemVar}->last = {$itemVar}->iteration === {$itemVar}->total;";
}
$output .= '?>';

Expand Down
41 changes: 41 additions & 0 deletions tests/UnitTests/TemplateSource/TagTests/For/CompileForTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,45 @@ public function dataTestSpacing()
array("{for \$x=-1;\$x>=0;\$x--}{\$x}{forelse}{\$buh}{/for}", "buh", 'T14', $i++),
);
}

/**
* Test {for} inside an inheritance (extends) template.
*
* The {for} tag pokes loop bookkeeping (step, total, value, ...) directly
* onto its loop Variable. In an extended template the child block renders
* with SCOPE_PARENT, so the loop variable is assigned to the parent and was
* not resolvable locally, causing 'assign property on null'.
*
* @see https://github.com/smarty-php/smarty/issues/1036
*
* @dataProvider dataForInheritance
*/
public function testForInheritance($code, $result, $testName, $caching)
{
$this->smarty->caching = $caching;
$tpl = $this->smarty->createTemplate($code);
$this->assertEquals($result, $this->smarty->fetch($tpl), "test - {$testName}");
}

public function dataForInheritance()
{
$parent = 'extends:string:{block name="content"}{/block}';
$cases = array(
array('to', '{for $i=0 to 3}{$i}{/for}', '0123'),
array('step', '{for $i=0 to 3 step 2}{$i}{/for}', '02'),
array('max', '{for $i=0 to 30 max=3}{$i}{/for}', '012'),
array('nested', '{for $i=0 to 1}{for $y=0 to 3}{$y}{/for}{/for}', '01230123'),
array('legacy', '{for $i=0; $i<4; $i++}{$i}{/for}', '0123'),
);
$data = array();
foreach (array(false, true) as $caching) {
foreach ($cases as $case) {
$code = $parent . '|string:{block name="content"}' . $case[1] . '{/block}';
$name = $case[0] . ($caching ? ' (caching)' : '');
$data[] = array($code, $case[2], $name, $caching);
}
}
return $data;
}

}
Loading