Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Nested control blocks

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

Page: 1
Go to End
Author Topic: 1384 Views
  • Laurie
    Avatar
    Community Member
    21 Posts

    Nested control blocks Link to this post

    I'm trying to create a series of regional pages that then list projects grouped by country in the particular region.

    - I have a country table in the db with the following three fields: ID, Name, RegionPageID.
    - I also have a projectpage table in the db with info about the projects.
    - And finally I have a projectpage_country table that relates the two: ID, ProjectPageID, CountryID

    I'm getting stuck on writing the foreach loop that gets the project info and combines it with the country info. None of the examples I'm finding show how to do this with two DataObjects.

       function Countries() {
          // get countries for the given region
          $countryList = DataObject::get (
             $callerClass = 'Country',
             $filter = "RegionPageID = '{$this->ID}'",
             $sort = 'name ASC'
          );
          
          $countryListIterator = $countryList->getIterator();
          
          // loop through countries
          foreach($countryListIterator as $country) {
             // if the country has projects associated with it
             // return the country name to the template along with the related projects (as a dataobject?)
          }
          
          // return something here
       }
       
       function Projects($countryID) {
          $where = "projectpage_country.countryID = '$countryID'";
          $join = "RIGHT JOIN projectpage_country ON ProjectPage_Live.ID = projectpage_country.projectPageID";
          return DataObject::get('ProjectPage', $where, 'endDate DESC, title ASC', $join);
       }

    Then in the template...

          <% control Countries %>
          <h3>$CountryName</h3>
             <% control Projects %>
                <% if first %>
                   <ul>
                <% end if %>
                      <li><a href="$Link">$Title</a> ($startDate.Year&ndash;$endDate.Year)</li>
                <% if last %>
                   </ul>
                <% end if %>
          <% end_control %>

    Any help would be most appreciated. Thanks.

    Cheers,
    Laurie

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: Nested control blocks Link to this post

    function Countries() {
    // get countries for the given region
    $countryList = DataObject::get (
    $callerClass = 'Country',
    $filter = "RegionPageID = '{$this->ID}'",
    $sort = 'name ASC'
    );
    ...
    $countryList->Projects = Projects($countryID);
    $countryList->Projects-> type = 'project';
    }

    Then u can use recursive function in page controller that will remake that object to one array of objects like that:

    $a = array ( $class1, $class1-1, $class2, $class2-2);

    Where $class1-1 and $class2-2 is $countryList->Projects;

    <% control RecursiveFunctionName() %>
    <% if type == project %>
    $projectPageID <!-- it's a Project --!>
    <% else %>
             $name <!-- it's a Country --!>
    <% end_if %>
    <% end_control %>

    Something like that =)

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