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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Layout function call problem


Reply


7 Posts   1096 Views

Avatar
outeredge

Community Member, 4 Posts

10 December 2009 at 12:46am

Hey,

I'm trying to call a function I made called DisplayCourse() which displays data from a data object but it's not working. The function is getting called in a layout file (Homepage.ss) but the function doesn't actually get called at all! It works in the main template file(Page.ss) but not in the layout.

Any help would be greatly appreciated.

Thanks!

Avatar
Fuzz10

Community Member, 787 Posts

10 December 2009 at 1:48am

You really need to explain a bit more / post some code...

It probably has something to do with the method being in the wrong class .....

Avatar
outeredge

Community Member, 4 Posts

10 December 2009 at 2:04am

Hey,

Thanks. I've posted the relevant code below. The code is just displaying the data from a data object called Course things like Date, title, location etc... on the front page. What's odd is that it's not even getting called when in the layout file.

Page.php
------------
class Page_Controller extends ContentController {

...

public function DisplayCourse(){
die('function called'); //To test if function is getting called
$courses = DataObject::get("Course");
$results = ($courses) ? DataObject::get("Course", "", "", "", "") : false;
return ($results) ? $results : false;
}

}

Layout/Homepage.ss
----------------------------

...

<% control DisplayCourse %>
<div>
<p><strong>$Date.Nice</strong></p>
<p>$Location</p>
<p class="purple"><strong>$Title</strong></p>
<p class="purple">$Description</p>
&rsaquo; <a href="#" class="read-more">Read more</a>
</div>
<% end_control %>

....

Course.php
--------------

<?php
class Course extends DataObject
{
   static $db = array (
      'Date' => 'Date',
'Location' => 'Text',
      'Title' => 'Text',
'Description' => 'Text'
   );

   static $has_one = array (
      'LearningAndDevelopmentPage1' => 'LearningAndDevelopmentPage1',
   );

   public function getCMSFields_forPopup()
   {
      return new FieldSet(
       new CalendarDateField('Date', 'Date of the course'),
new TextField('Location', 'Location of the course'),
new TextField('Title', 'Title of the course'),
new TextareaField('Description', 'Description of the course')
      );
   }
}

?>

Avatar
Fuzz10

Community Member, 787 Posts

10 December 2009 at 2:08am

Do you have a HomePage_Controller class as well ? If so , does it inherit from Page_Controller ?

Avatar
outeredge

Community Member, 4 Posts

10 December 2009 at 2:35am

Yeah I do.

Avatar
Fuzz10

Community Member, 787 Posts

10 December 2009 at 2:37am

Should work then..

Is the <% control DisplayCourse %> in the top context ? (not within another control block) ?

Avatar
outeredge

Community Member, 4 Posts

10 December 2009 at 2:47am

Ah ha, That was it! It was nested inside another control block.

Thanks Fuzz10! :-)