21281 Posts in 5729 Topics by 2600 members
| Go to End | Next > | |
| Author | Topic: | 299 Views |
-
Gridfield loop and count in template

25 February 2013 at 8:57pm
I have a gridfield and would like to count the objects and do something if count = 2.
eg:
<% loop Objects %>
$title
if ($count = 3)
<br /><% end_loop %>
Is the above possible?
-
Re: Gridfield loop and count in template

25 February 2013 at 10:02pm Last edited: 25 February 2013 10:05pm
<% if $Pos = 3 %>
Or if you mean the actual number of items in the list, you might be able to do:
<% if Objects.Count = 3 %>
Haven't tested that though, no idea if it'll work. If it doesn't work, you can create a function in your controller to count them and return true/false.
-
Re: Gridfield loop and count in template

25 February 2013 at 10:04pm
What you're looking for is $Pos or $MultipleOf.
<% loop Objects %>
$title<% if Pos = 3 %>
<br /> Position #3
<% end_if %><% if MultipleOf(3) %>
<br /> each third iteration
<% end_if %><% end_loop %>
http://doc.silverstripe.org/framework/en/reference/templates#looping-over-lists
-
Re: Gridfield loop and count in template

26 February 2013 at 1:05am
Thanks, just found the page too.
How about conditional statements?
<% if $Pos=="3" %> works
How about => or != I get errors when I use !=
"Sorry, there was a problem with handling your request."
-
Re: Gridfield loop and count in template

26 February 2013 at 5:43am
There's no built-in less than/greater than, you can implement that sort of thing by creating a function in your controller. For 'not', use:
<% if not $Pos=3 %>
See here: http://doc.silverstripe.org/framework/en/reference/templates#conditional-logic
-
Re: Gridfield loop and count in template

26 February 2013 at 9:58am
Hi Kinglozzer,
Do you have an example of how to write the custom functions in the controller?
-
Re: Gridfield loop and count in template

26 February 2013 at 1:39pm
Controller
function MoreThen() {
if($pos > $value) {
return TRUE;
} else {
return FALSE;
}}
Template
If the function is outside of the loop it works and returns "1"
$MoreThen(2,1)
<% loop GalleryObjects %>
<% end_loop %>
If the function is inside of the loop if does not work. No value is returned.<% loop GalleryObjects %>
$MoreThen(2,1)
<% end_loop %> -
Re: Gridfield loop and count in template

26 February 2013 at 1:48pm Last edited: 26 February 2013 1:54pm
Found it: $Top.MoreThen(2,1)
But $Pos doesn't seem to actually work.
There are 5 items so " 2 is more than 1" should be fired off twice. NOthing is returned.<% if Top.MoreThen($Pos,2) %>
2 is more than 1
<% end_if %>
| 299 Views | ||
| Go to Top | Next > |



