Newscoop 3 Cookbook

List of latest comments

The Newscoop internal statistics mechanism keeps track of the latest comments, and this mechanism can be used to create a list of the ten most recent comments on the site for all articles. The following code snippet is taken from The Journal's front page, from the sub-template set_thejournal/_tpl/front_tabs.tpl

It is displayed inside a jQuery tab which displays comments together with most read articles (read the chapter List of popular articles (most read) for instructions on how to make that template).

Here is a screenshot:

This code snippet will:

  • Set the current issue as the active environment
  • List five articles, ordered by the latest comment, in descending order
  • Set a constraint so that the article type is 'news'
  • Apply a different style for the recent comments
  • List a single comment, by date
  • List the nickname given by a commenter on a comment
  • Combine that with the name of the article they're commenting on
  • Make that article name appear in italic
<ul>
{{ local }}
{{ set_current_issue }}
{{ list_articles length="5" order="byLastComment desc" constraints="type is news" }}
    <li class="recentcomments">{{ list_article_comments length="1" order="bydate desc"}}{{ $gimme->comment->nickname }}{{ /list_article_comments }} on <a href="{{ uri options="article" }}" style="font-style: italic">{{ $gimme->article->name }}</a></li>
{{ /list_articles }}
{{ /local }}
</ul>

Here is another example which will do the following:

  • List the ten most recent comments, regardless of article
  • Order the comments by date, in descending order
  • Provide the URI for the comment and the article's name
  • Return the count of article comments
  • Return an excerpt of the article comments, truncated to 400 characters
  • Return the date and time the comment was posted
{{list_article_comments length="10" ignore_article="true" order="byDate desc"}}
    <a href="{{uri}}#comments">{{$gimme->article->name}}</a><sup>{{$gimme->article->comment_count}}</sup>
    <p>{{$gimme->comment->content|truncate:400}} <span>{{$gimme->comment->submit_date|camp_date_format:"%H:%i"}}</span></p>
{{/list_article_comments}}

Finally, here is a similar, but more limited approach. We will:

  • List the ten most recently-commented articles, ordered by last comment, in descending order, regardless of the issue and section they were published in
  • Return the article's URI
  • Return the article's name
  • Return the count of comments for the article
{{ list_articles length="10" order="byLastComment desc" ignore_issue="true" ignore_section="true" }}
  <a href="{{ uri }}#comments">{{ $gimme->article->name }}</a><sup>{{ $gimme->article->comment_count }}</sup>
{{ /list_articles }}

To learn more about listing comments for one specific article and providing the comment form, read the chapter on Article Comments.