silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 820 forks source link

Incomplete code for handling current-scope loop in templates #11250

Closed GuySartorelli closed 1 month ago

GuySartorelli commented 1 month ago

Found while working on https://github.com/silverstripe/silverstripe-framework/issues/11237

If you create a <% loop %> tag inside a template without including an argument (i.e. not <% loop $MyIterable %>), the template is generated with some very clearly incomplete code, which includes the placeholder "Foo".

Template:

<% loop %>
    // Inside the loop here
<% end_loop %>

Cached template (what actually gets executed):

$scope->obj('Up', null)->obj('Foo', null);
$scope->pushScope();
while (($key = $scope->next()) !== false) {
$val .= '    // Inside the loop here';
};
$scope->popScope();

This is handled by SSTemplateParser::ClosedBlock_Handle_Loop()

The relevant code was added in https://github.com/silverstripe/silverstripe-framework/commit/91f4ba15f15970638da5091639e35daae17f3c6f as part of https://github.com/silverstripe/silverstripe-framework/pull/209 and it seems pretty clear the intention was to loop over whatever is currently in scope (basically a shortcut for <% loop $Me %>) but that for whatever reason the feature was never fully implemented.

PRs

sabina-talipova commented 1 month ago

PRs merged. Task is completed. Close.