Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » [SOLVED] Accessing Children functions in Template

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

Page: 1
Go to End
Author Topic: 1776 Views
  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    [SOLVED] Accessing Children functions in Template Link to this post

    I'm sure I've been here before, but can't find any old posts.

    I have the following structure for a global ski resort website that I have inherited:

    1. A CountryPage
    2. A ResortPage (A child of CountryPage)
    3. A HotelPage (A child of ResortPage)

    Although these are related to each other in the site tree as actual pages, the information on the last one, HotelPage, is actually displayed one after the other on the ResortPage.

    So a simplified setup of ResortPage.ss is as follows:

    <!-- Resort Info -->
    $Name
    $Content

    <!-- Hotels in this Resort -->
    <% control Children %>
    $Name
    $Content
    $GetPricing
    <% end_control %>

    This is all working well except for the "GetPricing". This is a function on HotelPage.php that returns a HTML table using data from an element on the HotelPage called "Pricing". This is a TableField, which in turn is an extension of DataObject.

    This is the code in HotelPage.php

    function GetPricing() {
          $submitLink = DataObject::get("Pricing", "ThispriceID = $this->ID");   
          $output = $this->makeTable($submitLink);
          return $submitLink;
       }
       
    private function makeTable($PricingData) {
    // Code for intelligently creating table based on $PricingData
    {

    This works perfectly if I look at the HotelPage directly, but the function isn't called when I'm trying to list these pages on the ResortPage using <% control Children %>. So I must have a scope problem.

    Can anyone see a way around this?

    Cheers
    Aaron

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: [SOLVED] Accessing Children functions in Template Link to this post

    Some headway made:

    I put the GetPricing function in the Parent ResortPage.php instead, and called it using $Top.GetPricing.

    Now I need to work out how to send the ID of the current child to the function, which I beleive is a gotcha in SS. Ideally I want the command to be $Top.GetPricing({$ID}) however this doesn't pass the value, it simply outputs "(1529)" where the number IS the correct ID for the current record.

    Any thoughts?

    Cheers
    Aaron

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: [SOLVED] Accessing Children functions in Template Link to this post

    Got it with a work around.

    On the ResortPage.php file, under the ResortPageController class, I add a function called "getHotels". This function creates a custom DataObject, calls the function that creates the HTML pricing table, and adds that HTML as a field to the custom DataObject before returning.

    ResortPage.php

    function getHotels() {
       
          $CustomHotelPageSet = new DataObjectSet(); //Result dataset
          
          $HotelDataSet = DataObject::get("LocationHotelPage", "ParentID = " . $this->ID);
          foreach ($HotelDataSet as $HotelDataObject) {
    $PricingHTML = $this->getPricing($HotelDataObject->ID);
    $item = $HotelDataObject->customise(array('PricingHTML' => $PricingHTML ));
             $CustomHotelPageSet ->push($item);
          }

    return $CustomHotelPageSet;
       }

    function GetPricing($hotel_id) {
    $submitLink = DataObject::get("Pricing", "ThispriceID = " . $hotel_id);
    $output = $this->makeTable($submitLink);
    return $submitLink;
    }

    private function makeTable($PricingData) {
    // Code for intelligently creating table based on $PricingData
    {

    Then, on my template, it's as simple as this:

    ResortPage.ss

    <!-- Resort Info -->
    $Name
    $Content

    <!-- Hotels in this Resort -->
    <% control getHotels %> <!-- Note we no longer control Children here, but my custom function -->
    $Name
    $Content
    $GetPricing
    <% end_control %>

    Hope that helps someone

    Cheers
    Aaron

    1776 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.