Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » LatestNews not shown in HomePage

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: 1097 Views
  • andria
    Avatar
    Community Member
    7 Posts

    LatestNews not shown in HomePage Link to this post

    Hello,

    I am new to SS and I have a problem. I made the tutorial 2 using the code posted online, modifying both
    my HomePage.php and my HomePage.ss from the Layout folder in themes, but nothing happens. The news
    do not get displayed on my HomePage no matter how I change the code. I use my own theme, and not a
    SS pre-built theme. I post the code here:

    HomePage.php ***from mysite/code***

    function LatestNewsItems($num=5) {
    $news = DataObject::get_one("ArticleHolder");
    Debug::show($news);
    return ($news) ? DataObject::get("ArticlePage", "ParentID = '$news->ID'", "Date DESC", "", $num) : false;
    }

    HomePage.ss ***from themes/mytheme/templates/Layout***

    <div id="Header">
    <div id="Navigation">
    <% include Navigation %>
    </div>
    </div>
    <div id="Banner"></div>

    <div id="News">
    <div id="NewsList">
        <ul>
    <% control LatestNewsItems %>
    <li class="newsDateTitle"><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></li>
    <li class="newsDateTitle">$Date.Nice</li>
    <li class="newsSummary">$Content.FirstParagraph<a href="$Link" title="Read more on &quot;{$Title}&quot;">Read more &gt;&gt;</a></li>
    <% end_control %>
    </ul>
    </div>
    </div>
    <div id="Newsletter"> </div>         
       <div id="Content" class="typography">
        <div id="Layout1">
        $Content
        </div>
       </div>            
       <div id="Footer"></div>

    I tried flushing the page many times but nothing happens. Please help me out!
    Have a nice day!

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: LatestNews not shown in HomePage Link to this post

    Check the homepage is of type 'HomePage' in the CMS - Under the behaviour tab on the page you should see what class type it is. Make sure its set to the home one.

    Next thing to check is that LatestNewsItems is in the HomePage_Controller class in HomePage. You could also try and move the LatestNewsItems function to the Page_Controller class in Page.php and try it there

  • andria
    Avatar
    Community Member
    7 Posts

    Re: LatestNews not shown in HomePage Link to this post

    You were right. I misplaced the function under the final '}' of the function HomePage_Controller extends Page_Controller.
    The Home page is of HomePage type. Now the code looks like this:

    HomePage.php

    <?php

    class HomePage extends Page {
       
       public static $db = array(
       );
       
       public static $has_one = array(
       );
       
    }

    class HomePage_Controller extends Page_Controller {
       
       public function init() {
          parent::init();

          // Note: you should use SS template require tags inside your templates
          // instead of putting Requirements calls here. However these are
          // included so that our older themes still work
          Requirements::themedCSS("layout");
          Requirements::themedCSS("typography");
          Requirements::themedCSS("form");
       }
       
       /**
        * Site search form
        */
       function SearchForm() {
          $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search';
          $fields = new FieldSet(
           new TextField("Search", "", $searchText)
           );
          $actions = new FieldSet(
           new FormAction('results', 'Search')
           );

           return new SearchForm($this, "SearchForm", $fields, $actions);
       }
       
       /**
        * Process and render search results
        */
       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
           'Title' => 'Search Results'
           );

           return $this->customise($data)->renderWith(array('Page_results', 'Page'));
       }
       
       
        function LatestNewsItems($num=5) {
    $newssection = DataObject::get_one("News");
    Debug::show($newssection);
    return ($newssection) ? DataObject::get("Article01", "ParentID = '$news->ID'", "Date DESC", "", $num) : false;
    }

       
    }

    ?>

    I get an error though:

    [User Error] Bad class to singleton() - News
    GET /silverstripe/?flush=1

    Line 261 in C:\wamp\www\silverstripe\sapphire\core\Core.php
    Source

    252 *
    253 * @param string $className
    254 * @return Object
    255 */
    256 function singleton($className) {
    257    global $_SINGLETONS;
    258    if(!isset($className)) user_error("singleton() Called without a class", E_USER_ERROR);
    259    if(!is_string($className)) user_error("singleton() passed bad class_name: " . var_export($className,true), E_USER_ERROR);
    260    if(!isset($_SINGLETONS[$className])) {
    261     if(!class_exists($className)) user_error("Bad class to singleton() - $className", E_USER_ERROR);
    262       $_SINGLETONS[$className] = Object::strong_create($className,null, true);
    263       if(!$_SINGLETONS[$className]) user_error("singleton() Unknown class '$className'", E_USER_ERROR);
    264    }
    265    return $_SINGLETONS[$className];
    266 }
    267

    Trace

    * Bad class to singleton() - News
    Line 261 of Core.php
    * singleton(News)
    Line 2464 of DataObject.php
    * DataObject::get_one(News)
    Line 56 of HomePage.php
    * HomePage_Controller->LatestNewsItems()
    * call_user_func_array(Array,Array)
    Line 318 of ViewableData.php
    * ViewableData->obj(LatestNewsItems)
    Line 50 of .cacheC..wamp.www.silverstripe.themes.riffinsolvency.templates.Layout.HomePage.ss
    * include(C:\Windows\Temp\silverstripe-cacheC--wamp-www-silverstripe\.cacheC..wamp.www.silverstripe.themes.riffinsolvency.templates.Layout.HomePage.ss)
    Line 354 of SSViewer.php
    * SSViewer->process(HomePage_Controller)
    Line 346 of SSViewer.php
    * SSViewer->process(HomePage_Controller)
    Line 175 of Controller.php
    * Controller->handleAction(HTTPRequest)
    Line 129 of RequestHandler.php
    * RequestHandler->handleRequest(HTTPRequest)
    Line 122 of Controller.php
    * Controller->handleRequest(HTTPRequest)
    Line 29 of ModelAsController.php
    * ModelAsController->handleRequest(HTTPRequest)
    Line 44 of RootURLController.php
    * RootURLController->handleRequest(HTTPRequest)
    Line 277 of Director.php
    * Director::handleRequest(HTTPRequest,Session)
    Line 121 of Director.php
    * Director::direct(/)
    Line 118 of main.php

    Can you please help me out?

    Nice day, to everybody!

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: LatestNews not shown in HomePage Link to this post

    $newssection = DataObject::get_one("News");

    So you have a 'News.php' file which contains a News class right?

  • andria
    Avatar
    Community Member
    7 Posts

    Re: LatestNews not shown in HomePage Link to this post

    I figured it out, it's working now! I shouldnt change the page types "ArticleHolder" and "ArticlePage" from
    the function to the page names because SS find them out automatically.

    But thanks for pointing it out to me: I was having a headache for 2-3 days trying to figure out why it was not
    working to find out that it was a misplaced '}'.

    Nice day to you!

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