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.

Template Questions /

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

How to insert code after every third item?


Go to End


16 Posts   9695 Views

Avatar
bennettpr

Community Member, 37 Posts

2 March 2009 at 11:53am

Thanks Sam,

Very helpful (and fast!)

Paul

Avatar
Sam

Administrator, 690 Posts

2 March 2009 at 12:04pm

Yeah I've hooked the forum's RSS feed into http://notify.me so that I get IM notifications of posts; it can be slightly overwhelming at times but it makes sure that I have my finger on the pulse of the forum a little more.

Avatar
superautomatic

Community Member, 53 Posts

2 March 2009 at 11:25pm

Thanks a lot, worked like a charm.

To insert the clearing div after every third item i had to add 1 to iteratorPos (since it starts on 0), so my function ended up as

	function newShelf() {
	return ((($this->iteratorPos + 1) % 3) == 0);
	}

/jens

Avatar
NickJacobs

Community Member, 148 Posts

4 March 2009 at 6:27pm

Hi, I'm trying to use this with UncleCheeses DataObjectManager to put a clearer div after every fourth iteration, but having the same trouble where inside the control loop I cant access the newShelf function. This is what I've got:

In PeoplePage.ss

<% if People %>
    <% control People %>	

	  <div class="PersonPod">
	  <div class="PersonImage">$PersonPic.SetWidth(130)</div>
      <div class="PersonName">$PersonName</div>
      <div class="PersonTitle">$PersonTitle</div>
       </div>
	   <% if newShelf %>
	   <div class="clearer">output this</div>
	   <% end_if %>
    <% end_control %>
 <% end_if %>

Inside the PeoplePage class:

function newShelf() { 
   return ((($this->iteratorPos + 1) % 4) == 0); 
   }

I've tried outputting $iteratorPos directly in the page template and that works, but no output of newShelf inside the control loop. Any ideas?

Avatar
Carbon Crayon

Community Member, 598 Posts

5 March 2009 at 3:34am

Hi Tsunami

You need to put your newShelf() function inside your People model class. IT makes ense if you think about it because what the <% control People %> is doing is essentailly putting you in the context of those DataObjects so any functions you call from within that control need to be available from within that object.
Just for completeness if you wanted to step out of that control block to access something on the top level page controller/model then you could use the $Top variable by calling $Top.FunctionName. As far as I can tell this doesn't work in controllers though so you couldn't do <% control top.FunctionName %>.

Avatar
NickJacobs

Community Member, 148 Posts

5 March 2009 at 9:09am

Haha...how easy it all seems when someone points it out!

thanks aram, that cracked it.......

Avatar
Artyom

Community Member, 22 Posts

2 May 2009 at 9:39am

Can we use a similar logic for Menu iteration? I would rather not subclass the Menu, since I don't control its creation. I am making a simple footer nav, and would like to do something like

<% control Menu(1) %>

<li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle</a>
<% if Iterator.peekNext %>
&nbsp;|&nbsp;
<% end_if %>
</li>

<% end_control %>

To no avail. Also tried Children.Iterator.peekNext

I get "Call to undefined method DataObjectSet_Iterator::cachedCall()"

... this is a common task, isn't it? Is there a better way?

thanks,
Artyom

Avatar
dab

Community Member, 50 Posts

4 June 2009 at 12:53am

Artyom, i think you may use code like this:

<% control Menu(1) %>   
   <li><a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">$MenuTitle</a> 
   <% if Last %>
   <% else %> &nbsp;|&nbsp; 
   <% end_if %> 
   </li>
<% end_control %>

Go to Top