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

Is there a control to detect every nth list item


Go to End


23 Posts   11063 Views

Avatar
Hammy

Community Member, 49 Posts

17 February 2009 at 7:17am

Edited: 17/02/2009 7:23am

Awesome, that worked :)

My only concern about adding it into the /sapphire/code/ViewableData.php is that when I go to update the SS with another release, this could easily be forgotten about. Is there a way to extend this?

I've tried adding the following into my _config.php

Object::add_extension('ViewableData', 'ExtendedViewableData');

and in /mysite/code/ExtenedViewableData.php

<?php
class ExtendedViewableData extends Extension { 
	
	/**
	 * Returns 'true' if this item is after 4th item.
	 * @return boolean
	 */
	function IsAfterFourth() { 
		return $this->iteratorPos % 4 == 0; 
	}
	
}
?>

But this doesn't seem to work and I get the following error:

"[User Error] Object::__call() Method 'loadExtraStatics' not found in class 'ExtendedViewableData'"

Have I got this all wrong and need to approach it from another angle?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

17 February 2009 at 1:31pm

There is absolutely no reason why you should have to hack the core to do this. Add the function I gave you to the object you're controlling. For instance:

<% control MyObject %>
<% if Fourth %>Fourth<% end_if %>
<% end_control %>

class MyObject
{
function Fourth()
{
return !$this->iteratorPos % 4;
}
}

Avatar
Trym

Community Member, 18 Posts

17 February 2009 at 7:17pm

Hi UncleCheese.

If the object that is in control is a DataObjectSet returned from e.g. the method call "DataObject::get", then its more troblesome to add the desired functionality.

Do you have a suggestion for this?

Best regards Trym

Avatar
Hammy

Community Member, 49 Posts

17 February 2009 at 9:49pm

Sorry - should have mentioned that I'm using DataObject::get method as the object inside the control...

Avatar
UncleCheese

Forum Moderator, 4102 Posts

18 February 2009 at 3:23am

Right.. so this:

class SomePage extends Page
{
function Objects()
{
return DataObject::get("SomeObject");
}
}

class SomeObject
{
function Fourth() { return !$this->iteratorPos % 4;}
}

<% control Objects %>
<% if Fourth %>fourth<% end_if %>
<% end_control %>

Shouldn't need anything more than that. I do this all the time.

Avatar
Trym

Community Member, 18 Posts

18 February 2009 at 3:49am

cool.

\trym

Avatar
reekrd

Community Member, 9 Posts

26 June 2009 at 7:17am

Hi.

I'm trying to apply this to the second tutorial, http://doc.silverstripe.org/doku.php?id=tutorial:2-extending-a-basic-site

My setup is exactly as in the tutorial and for every third news item I would like to add a divider or set a special css class. I just can't figure it out :(

This is how my HomePage.ss looks like:

<div id="NewsWrapper" class="content {$URLSegment}">
<ul id="NewsList">
<% control LatestNews %>
<li class="newsitem">
<h1><a href="$Link" title="Read more on &quot;{$Name}&quot;">$Title</a></h1>
<h3>$Date.Nice</h3>
<p>$Content.FirstParagraph <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></p>
</li>
<% end_control %>
</ul>
</div> <!-- end: NewsWrapper -->

In this thread I saw that there is a template variable called $Pos that gives you the current position. However I can't figure out how to use it. It seems logical that I could simply do:

<% control LatestNews %>
<% if (($Pos%3)==0) %>
<li class="newsitem">
<% else %>
<li class="newsitem">
<% end_if %>
<h1><a href="$Link" title="Read more on &quot;{$Name}&quot;">$Title</a></h1>
<h3>$Date.Nice</h3>
<p>$Content.FirstParagraph <a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></p>
</li>
<% end_control %>

But that gives me a blank page. I can't seem to use it in any way when I combine it with an if statement. It would be awesome if the tutorials also would cover things like this. Tutorials that are more from a designers point of view.

Maybe there is a section in the documentation or tutorials that covers things like this. If not it would be very helpful to collect all these types of tips and tricks so it's easier for a designer to jump straight into using SilverStripe without really having to understand the inner workings of it....

Best
.r

Avatar
UncleCheese

Forum Moderator, 4102 Posts

26 June 2009 at 7:32am

Assuming you're using the same classes that are in the tutorial, you can just add to your ArticlePage class:

public function Third()
{
return !$this->iteratorPos % 3;
}

then replace:

<% if (($Pos%3)==0) %>

with

<% if Third %>