Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Upgrading SilverStripe /

Ask questions about upgrading SilverStripe to the latest version.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Strange template include/loop order in SS3


Go to End


2 Posts   2023 Views

Avatar
blueskies

Community Member, 44 Posts

3 January 2013 at 12:24am

Edited: 08/01/2013 7:51am

Okay, I have run into the following predicament and I'm not sure if this is a bug or intended behaviour, but it affects quite a bit of a project I'm upgrading.

In a template I have the following code:

<% if Results %>
    <table class="records items">
        <% loop Results %>
                    LOOP: $Pos               
                    <% include ResultsTable %>
                                   
        <% end_loop %>               
    </table>
<% end_if%>

Which grabs the results from a search and plops them into a template. But the table part I reuse in other areas, so it is included. The table include looks like this:

 <% if First %>
	<tr class="heading">
		<th>INCLUDE $Pos</th>

		<% control ColHeadings %>
		<th>$Value</th>
		<% end_control %>
	</tr>
<% end_if %>

	<tr<% if Even %> class="zebra"<% end_if %>>
		<td></td>
		<% control RowValues %>
			<td>$Value</td>
		<% end_control %>
	</tr>

All the code works except I'm getting an unintended result: Every time the include runs, the "if first" evaluates to true... every time. The previous (2.*) versions of Silverstripe would first include the resultstable and THEN loop over it, with the "if first" only being true one time. The new (3.0.3) version seems to include that template every time it loops, which sets the iterator back to one every time.

Notice the $Pos vars in the templates: LOOP $Post evaluates to the correct number (LOOP 1, LOOP 2, LOOP 3, etc). But the INCLUDE $Pos is always 1.

Is this a bug or intended behaviour? If it is intended, then how am I supposed to order the includes/loop in order to have "if first" evaluate true only once?

-----------------
Don't know if it's relevant, but the table is populated by the following code:

....
$query = $context->getQuery($searchCriteria, $Sort, array('start'=>$start,'limit'=>$limit));
$records = $context->getResults($searchCriteria, $Sort);
if($records) {
    $records = new PaginatedList($records, $request);
}       
return $records;

Avatar
blueskies

Community Member, 44 Posts

8 January 2013 at 7:49am

Please.... anybody? :-)