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

Iterate through all fields of one record


Go to End


2 Posts   2254 Views

Avatar
MattB

Community Member, 7 Posts

19 November 2010 at 1:40am

Edited: 19/11/2010 1:41am

Hi all,

I'd like to iterate through all the fields of a single record in an SS template. As a simple example, I'm trying to automatically output all the fields of a Member record.

SomePage_Controller.php

function MemberData() {
    return Member::CurrentUser();
}

SomePage.ss

<% control MemberData %>
    <% control Children %>
        $key: $value
    <% end_control %>
<% end_control %>

I've tried various permutations of returning DataObject, GetIterator(), ArrayData(), DataObject::database_fields, DataObjectSet, but I'm unable to find any way to iterate through the fields as Children (or otherwise)...

Would very much appreciate some extra brains on this one... any guidance?

Avatar
swaiba

Forum Moderator, 1899 Posts

19 November 2010 at 2:52am

how about...

SomePage_Controller.php

function MemberFields() { 
	return Member::CurrentUser()->getCMSFields(); 
}

SomePage.ss

<% control MemberFields %>
	$Title  $Value
<% end_control %>

(note untested)