Jump to:

3443 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Control Inside Control?

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

Page: 1
Go to End
Author Topic: 1659 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    Control Inside Control? Link to this post

    Hi,

    I've run into this problem again and again and again-- I'm controlling a DataObject::get() resultset, and while in that loop I need to get an ID each time and pass that into another function to get some data from an associated table to get some other stuff. Why can I not do this?

    function Clients($limit=5) {
          $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
          if($limit==5)
             return DataObject::get("ClientPage", "", 'Sort ASC', "", "$start,$limit");
          else
             return DataObject::get("ClientPage", "", 'Sort ASC', "", $limit);
    }

    In the template:

    <% control Clients %>
             <li>
                <% if ClientLogo %>$ClientLogo.SetWidth(100)<% end_if %>
                <% if ClientURL %><a href="$ClientURL" target="_blank"><% end_if %><h4>$ClientName</h4><% if ClientURL %></a><% end_if %>
                <% if ClientHeadline %><h6>$ClientHeadline</h6><% end_if %>

    <% control Testimonials(ClientID) %>
    Blah blah blah
    <% end_control %>
             </li>
    <% end_control %>

    This is on a Holder page. I need to display all the properties of the child pages, PLUS several rows of other data that BELONG to each of these pages that is stored in another table (using DataObjectManager). How else am I going to do this without knowing what ID I'm on in the loop? I can't put the Clients() function in the ClientPage class because I need to control it on the Holder page.

    Any advice on this would help a lot, because I have run into this issue on more than once occasion.

    Thanks,
    Garrett

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Control Inside Control? Link to this post

    You can't pass a $Var variable into a control or any statement in SS. It has to be a static value eg 150 or 'Test'. In your case how you would do this is on the Client dataobject have a function

    // Client Dataobject

    function Testimonials() {
    return DataObject::get('Testimonials', "ClientID = '$this->ID'");
    }

    then in your template you can do

    <% control Clients %>
    $Title
    // etc
    <% control Testimonials %>

    <% end_control %>
    <% end_control %>

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Control Inside Control? Link to this post

    Hi thanks for your reply @willr.

    What you described is already what I am doing. I already DO have the Testimonials function in the ClientPage class:

    ...
    class ClientPage_Controller extends Page_Controller {
       
       function Testimonials() {
          return DataObject::get("Testimonial", "ClientPageID = $this->ID", 'Sort ASC', "", null);
       }
       
    }

    The problem in this case is that the ClientPage is not a DataObject. The ClientPage class extends Page. These are Pages. Again, in the Holder template, I am controlling the ClientPages. How do I get these ID's and pass them into the Testimonial table to get these rows?? As you can infer from the above code, the Testimonial table is a DataObjectManager table whose rows are associated with a ClientPage based on a ClientPageID. But it doesn't work. When I put the nested Testimonial control in the template as follows:

    <% control Clients %>
    <li>
       <% if ClientLogo %>$ClientLogo.SetWidth(100)<% end_if %>
       <% if ClientURL %><a href="$ClientURL" target="_blank"><% end_if %><h4>$ClientName</h4><% if ClientURL %></a><% end_if %>
    <% if ClientHeadline %><h6>$ClientHeadline</h6><% end_if %>
                
       <% control Testimonials %>
       $TestimonialQuote<br />
       <% end_control %>
                
       </li>
       <% end_control %>

    I get:

    Website Error
    There has been an error

    The website server has not been able to respond to your request.

    What am I doing wrong here?

    Thanks again,
    Garrett

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Control Inside Control? Link to this post

    Never mind, @willr! I don't know what was wrong, but this code I had IS in fact working. I guess my browser was cached or something. Anyways, thanks so much for the help, as always!

    //Garrett

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Control Inside Control? Link to this post

    Website Error
    There has been an error

    FYI it might help when you are developing to put the site into development mode by adding Director::set_environment_type("dev"); in your _config file.

    Glad you got it sorted though.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Control Inside Control? Link to this post

    "The problem in this case is that the ClientPage is not a DataObject"

    Just an FYI: ClientPage extends Page extends SiteTree extends DataObject.. so Pages are definitely DataObjects =)

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Control Inside Control? Link to this post

    Yep! I got that now You're the Man, Hamish! Thanks so much!

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