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.

Archive

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

access to fields


Reply

5 Posts   1467 Views

Avatar
Nicolaas

17 October 2007 at 12:53pm Forum Moderator, 213 Posts

Hi Folk

I have the following function:

class eventsPage_Controller extends Page_Controller {
public function eventsItems() {
$Events = DataObject::get("eventSubmissionDataObject", "ID > 0 ", "EventDate DESC");
return $Events;
}
}

I access this on my template using the following:

<% control eventsItems %>
<li>
<div class="eventDate">$EventDate</div>
</li>
<% end_control %>

Works a treat. However, I would like to manipulate the data in this $Events object (i.e. the individual fields) such as EventDate and then return an html snippet rather than using the method above.

Can you tell me how to do this?

Thanks a million

Nicolaas

Avatar
Sean

17 October 2007 at 5:10pm (Last edited: 17 October 2007 5:20pm), Forum Moderator, 921 Posts

Hi there,

When you say 'html snippet', do you want the php to return the html, instead of doing it in the template? e.g.

PHP:

function MyObjects() {
$objects = DataObject::get('Events');
if($objects) {
$output = '<ul id="Objects">';
foreach($objects as $object) {
$customTitle = "This is my cool title called $object->Title";
$output .= "<li>$object->ID - $customTitle</li>\n";
}
$output .= '</ul>';
}
return $output;
}

While in the foreach loop you can just output whatever you like by calling fields or methods on $object like $object->ID or $object->Title, if that's what you meant.

The HTML is then simply this:

HTML:

$MyObjects

Not the best example of separation of HTML from PHP code, but it works pretty well for a simple listing like a <ul> with <li> elements inside.

Cheers,
Sean

Avatar
Nicolaas

18 October 2007 at 12:46am Forum Moderator, 213 Posts

Sean ! wow, so simple - SUCH a beaty. Thank you.... Works a treat

Avatar
Ingo

18 October 2007 at 8:54am Forum Moderator, 801 Posts

depending what you're trying to do, you can build unordered lists even simpler (with nesting even):
DataObjectSet->UL()
DataObjectSet->buildNestedUL()

Avatar
Nicolaas

18 October 2007 at 8:57am Forum Moderator, 213 Posts

wow, that is cool. Will look into this. Thank you. For the present exercise, I am actually sending the data to a different class to make a calendar. I just used the list as an example, because that is so common = > definitely will make use of that function.

Thank you

Nicolaas