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

What do I need and how do I do this on Silverstripe?


Reply

3 Posts   519 Views

Avatar
Lemonie

24 October 2012 at 10:42pm Community Member, 70 Posts

I am still trying to learn how to do things on Silverstripe but the hardest thing when you are teaching yourself is what you need to ask for!?

I want to create pages with articles/news items on as shown in this link ...

http://www.jerrygreendogs.org.uk/NewsCentre/List/News

... say 5 pages and then a main page with feeds on as shown in this link ...

http://www.jerrygreendogs.org.uk/NewsCentre/News

I have absolutely no idea where to start!! Please could you point me in the right direction and keep it simple, I am in the early stages of the learning process :-)

Avatar
Johan

24 October 2012 at 11:12pm (Last edited: 24 October 2012 11:59pm), Community Member, 49 Posts

It's so easy when you know how. The problem I also find with SilverStripe is finding the information, especially if you are not a programmer or part of the team.

Presuming you are using SS3 and you added your news articles to the CMS page with a date, I create a news template with...

static $db = array(
      'Date' => 'Date'
);

and a nice way to show the date in the CMS...

   function getCMSFields() {
      $fields = parent::getCMSFields();   
         
         $datefield = new DateField('Date', 'Publication date');
         $datefield->setConfig('showcalendar', true);
         $datefield->setConfig('showdropdown', true);
         $datefield->setConfig('dateformat', 'dd/MM/YYYY');            
         $fields->addFieldToTab('Root.Main', $datefield, 'Content');

...

You need to display snippets from the page above in the tree then create a template called NewsHolder simply in the SS file template add...

<% loop Children %>

<h2>$Title</h2>
<div class="date">
$Date.Nice
</div>
<p>$Content.FirstParagraph </p>
<p><a href="$Link" class="more">Read more</a></p>

<% end_loop %>

..but if you want to diplay snippets on another template like the homepage you need to create a function I call it LatestNews function and call this from the SS. The function will call the NewsHolder you created above.

<% loop LatestNews %>
<div class="news-item jlink">
<h2>$Title</h2>
<p><span class="date">$Date.Nice</span><br>
<a class="more" href="$Link">Read more</a> - $Content.FirstSentence</p>
</div>
<% end_loop %>

...and the function you need goes is the PHP of the template in the mysite/code such as hompage.php inside the class HomePage extends Page {...

   // show news
   function LatestNews($num=2) {
      $news = DataObject::get_one("NewsHolder");
      return ($news) ? DataObject::get("NewsArticle", "ParentID = $news->ID", "Date DESC", "", $num) : false;
   }

That should be more than enough to get you going if not go back to the basic tutorials. Good luck.

p.s. I would re-write the topic title to explain the content of the posts so that others know what this thread is about.

Avatar
stallain

27 October 2012 at 2:44pm Community Member, 61 Posts

Hi,

The query is even easier to write since SS3 :

function LatestNews() {
return NewsArticle::get()->sort('Date','DESC')->limit(2);
}