Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » iteration count in templates

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1974 Views
  • qhoxie
    Avatar
    Google Summer of Code Hacker
    39 Posts

    iteration count in templates Link to this post

    i made a quick attempt at implementing this in ssviewer, but i was unsuccessful, so before i pour more time into this, im curious if anyone has some insight into the best way to do this

    thanks

  • qhoxie
    Avatar
    Google Summer of Code Hacker
    39 Posts

    Re: iteration count in templates Link to this post

    ok well i got it working, but i am skeptical of the quality of my implementation

    it goes as follows:
    In SSViewer.php
    around line 191:
    '/\$Iteration/' => '<?= {dlr}key ?>',

    this is added as the first replacement case, so it maps a call of $Iteration in a template to $key

    also, the control foreach loops in SSViewer are changed from foreach($loop as $item) to foreach($loop as $key => $item) so that the $key is tracked, thus giving the iteration number

    this does, of course raise the issue of reserving the $Iteration variable

    this is confirmed as being functional in a few cases that meet my needs, but please give me your feedback, it could be a hack

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: iteration count in templates Link to this post

    This does seem like something that would be better implemented as a method on ViewableData.

    In fact, the skeleton of it is already there: $this->iteratorPos is a numeric value used to create boolean variables such as $Even, $Odd, $First, $Last. It's populated by a call to ViewableData::iteratorProperties() by the DataObjectSet object that contains each item.

    That should give you enough to go on, let me know what you come up with ;-)

    1974 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.