Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

Control Inside Control?


Reply

7 Posts   1667 Views

Avatar
Garrett

14 October 2009 at 10:28am Community Member, 245 Posts

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

Avatar
Willr

14 October 2009 at 1:15pm Forum Moderator, 5511 Posts

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 %>

Avatar
Garrett

15 October 2009 at 2:29am Community Member, 245 Posts

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

Avatar
Garrett

15 October 2009 at 2:50am Community Member, 245 Posts

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

Avatar
Willr

15 October 2009 at 9:00am Forum Moderator, 5511 Posts

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.

Avatar
Hamish

15 October 2009 at 1:09pm Community Member, 712 Posts

"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 =)

Avatar
Garrett

16 October 2009 at 4:03am Community Member, 245 Posts

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