Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » access to fields

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

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

Page: 1
Go to End
Author Topic: 1458 Views
  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    access to fields Link to this post

    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

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: access to fields Link to this post

    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

  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    Re: access to fields Link to this post

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

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: access to fields Link to this post

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

  • Nicolaas
    Avatar
    Forum Moderator
    213 Posts

    Re: access to fields Link to this post

    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

    1458 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.