Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Layout function call problem

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

Page: 1
Go to End
Author Topic: 1050 Views
  • outeredge
    Avatar
    Community Member
    4 Posts

    Layout function call problem Link to this post

    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!

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: Layout function call problem Link to this post

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

  • outeredge
    Avatar
    Community Member
    4 Posts

    Re: Layout function call problem Link to this post

    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')
          );
       }
    }

    ?>

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: Layout function call problem Link to this post

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

  • outeredge
    Avatar
    Community Member
    4 Posts

    Re: Layout function call problem Link to this post

    Yeah I do.

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: Layout function call problem Link to this post

    Should work then..

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

  • outeredge
    Avatar
    Community Member
    4 Posts

    Re: Layout function call problem Link to this post

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

    Thanks Fuzz10!

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