17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1352 Views |
-
access to fields

17 October 2007 at 12:53pm
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
-
Re: access to fields

17 October 2007 at 5:10pm Last edited: 17 October 2007 5:20pm
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 -
Re: access to fields

18 October 2007 at 12:46am
Sean ! wow, so simple - SUCH a beaty. Thank you.... Works a treat
-
Re: access to fields

18 October 2007 at 8:54am
depending what you're trying to do, you can build unordered lists even simpler (with nesting even):
DataObjectSet->UL()
DataObjectSet->buildNestedUL() -
Re: access to fields

18 October 2007 at 8:57am
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
| 1352 Views | ||
|
Page:
1
|
Go to Top |


