Hello.
When building a site for a client I have found myself writing a couple of utility functions that extend the somewhat meagre syntax available in Silverstripes template language.
Generally these functions end up inte Page class since different sub classes use them but I'm feeling that I'm cluttering up my page object because of this.
After reading about object decorators this sounded like just the thing. Instead of bloating my page class I could decorate the relevant classes (SiteTree and DataObject) with my utility methods. This would clean up the Page class and make it easier for me when adding/modifying utility functions since I won't have to dig through the page class file.
However my attempts att DataObject decoration has so far failed. I tried decorating the DataObject with my Nth-function (code below)
public function Nth($val) {
return ($this->owner->iteratorPos + 1) % $val == 0;
}
a really useful function that lets you modify output at regular intervals. However this code does not behave the way it does when it's in the page class even though it should (if I understand the documentation correctly). IteratorPos is a part of DataObject and I'm decorating DataObject but when my classes (that extend DataObject) try to use it it doesn't work.
I got the same results when I tried decorating SiteTree with utility functions. Actually, scratch that. Decorating SiteTree with
public function CurrentOrSection() {
return ($this->owner->isCurrent() || $this->owner->isSection()) ? true : false;
}
actually works which makes this even weirder.
Is it because I'm trying to access the iterator that I'm getting these odd results?
Kindly,
Marcus