Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » 0,1 and more comment/s - again

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

Page: 1
Go to End
Author Topic: 1363 Views
  • mc
    Avatar
    Community Member
    5 Posts

    0,1 and more comment/s - again Link to this post

    Hello,

    I do have the same problem as Typopunk about how to display "no comments", "1 comment" and "x commentS". Unfortunately he didn't wrote the solution back into the forum. If someone knows the solution - please let me (us) know.

    Thanks, MC

    PS: Here is the link to Typopunks original post: http://silverstripe.org/archive/show/190741#post190741

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: 0,1 and more comment/s - again Link to this post

    <% if Comments %>
    <% if Comments.Count = 1 %>
    <p>1 Comment<p>
    <% else %>
    <p>$Comments.Count Comments</p>
    <% end_if %>
    <% else %>
    <p>No Comments</p>
    <% end_if %>

    Should work

  • mc
    Avatar
    Community Member
    5 Posts

    Re: 0,1 and more comment/s - again Link to this post

    Thanks willr for your quick answer. I tried something like <% if Comments.Count = 1 %> as well.
    Unfortunately following error occurs:

    Parse error: syntax error, unexpected '}' in /tmp/silverstripe-cache-var-www-nena/.cache.var.www.nena.mysite.templates.Layout.ArticleHolder.ss on line 83

    If you leave out "=1" the error doesn't show up - of course the result isn't correct.

    Do you have any idea?

    MC

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: 0,1 and more comment/s - again Link to this post

    I solved this to move the counting to the model:

    function CartTotal(){
          if(!ShoppingCart::getCart()){
             return _t('Webshop.EMPTYCART','Your shoppingcart is empty');
          } else {
             if(count(ShoppingCart::getCart()) == 1){
                return sprintf(_t('Webshop.FILLEDCARTONE','You have %s product in your shoppingcart'), count(ShoppingCart::getCart()));
             } else {
                return sprintf(_t('Webshop.FILLEDCART','You have %s products in your shoppingcart'), count(ShoppingCart::getCart()));
             }
          }
       }


    Little bit different situation, but it should get you going...

  • mc
    Avatar
    Community Member
    5 Posts

    Re: 0,1 and more comment/s - again Link to this post

    Hi Martijn,

    Thanks for your answer. I tried to access the function from within the model of the holder but for whatever reason I don't receive any data. As I'm still a beginner with Silverstripe, I'm sure I'm doing something wrong.

    I extended the Model and the Controller. Here is a very simply example of what I tried just to make sure that I'm able to receive information directly.

    function class ArticleHolder extends Page {

    function getTest() {
    return "THIS IS A TEST";
    }

    }

    class ArticleHolder_Controller extends Page_Controller {

    function getTest() {
    return "THIS IS A TEST";
    }

    }

    In the View I entered:

    $getTest

    Actually this works very well with ArticlePage, but not with ArticleHolder.

    Any idea?

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: 0,1 and more comment/s - again Link to this post

    This should work. Did you flush after you editted the template?

  • mc
    Avatar
    Community Member
    5 Posts

    Re: 0,1 and more comment/s - again Link to this post

    Hi Martjin,

    I tried it - but it didn't work. But I found out why it didn't work...
    $getTest [/] was in the wrong "place".

    With your help I found the solution:

    class ArticlePage extends Page {

    function getCommentCounter() {
    $comments = DataObject::get("PageComment", "ParentID = " . $this->ID);
    if ($comments) {
    if ($comments->Count() == 1) return "1comment";
    else return $comments->Count() . "comments";
    } else return "no comments";
    }

    In the Holder-Template you just have to call $getCommentCounter.

    Thanks for your help

    MC

    }

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