Custom iterator functions in SS3

Showing 1-2 of 2 messages
Custom iterator functions in SS3 Uncle Cheese 7/10/12 7:53 AM
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?
Re: [silverstripe-dev] Custom iterator functions in SS3 Hamish Friedlander 7/10/12 2:31 PM
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.