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?
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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?
<% 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.
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
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."
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
Hi Kinglozzer,
Do you have an example of how to write the custom functions in the controller?
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 %>
<% loop GalleryObjects %>
$MoreThen(2,1)
<% end_loop %>
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 %>