Custom iterator functions in SS3

324 views
Skip to first unread message

Uncle Cheese

unread,
Jul 10, 2012, 10:53:40 AM7/10/12
to silverst...@googlegroups.com
In SS 2.x, we could write custom iterator functions in our ViewableData objects.

function PosIsGreaterThan($num) {
return $this->getIterator()->Pos() > $num;
}


All of that stuff has been moved out of the ViewableData class and into SSViewer_Scope. I have no idea how to get to the SSViewer_Scope object from a ViewableData object. Any thoughts?

Hamish Friedlander

unread,
Jul 10, 2012, 5:31:14 PM7/10/12
to silverst...@googlegroups.com
Hi,

Do you want to define PosIsGreaterThan, or call it from PHP code directly instead of from in a template?

If you mean call it from PHP code rather than a template, at the moment you can't - there isn't an API to do it. I was thinking about exposing the current scope - may even have discussed it on the list - but so far that hasn't happened.

But if you want to define PosIsGreaterThan, then do this:

class MyCustomIteratorFunctions implements TemplateIteratorProvider {

protected $iteratorPos;
protected $iteratorTotalItems;

public static function get_template_iterator_variables() {
return array('PosIsGreaterThan');
}

public function iteratorProperties($pos, $totalItems) {
$this->iteratorPos        = $pos;
$this->iteratorTotalItems = $totalItems;
}

function PosIsGreaterThan($num) {
return $this->iteratorPos > $num;
}
}

In 2.4 the concept of "object" and "member of collection" were merged together, messily. In 3 we've separated the concepts, hence <% loop %> and <% with %>, etc.

Hamish Friedlander | CTO
SilverStripe
http://www.silverstripe.com/

Phone: +64 4 978 7330 Extension 51
Skype: hamish.friedlander

Loft 503
Achilles House, 8 Commerce Street
Auckland City
New Zealand




--
You received this message because you are subscribed to the Google Groups "SilverStripe Core Development" group.
To view this discussion on the web visit https://groups.google.com/d/msg/silverstripe-dev/-/ybfcofQjnT8J.
To post to this group, send email to silverst...@googlegroups.com.
To unsubscribe from this group, send email to silverstripe-d...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/silverstripe-dev?hl=en.

Reply all
Reply to author
Forward
0 new messages