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.

Customising the CMS /

0,1 and more comment/s - again


Reply


7 Posts   1442 Views

Avatar
mc

Community Member, 5 Posts

6 August 2009 at 6:40am

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: [url]http://silverstripe.org/archive/show/190741#post190741[/url]

Avatar
Willr

Forum Moderator, 5513 Posts

6 August 2009 at 8:50pm

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

Avatar
mc

Community Member, 5 Posts

7 August 2009 at 7:21am

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

Avatar
Martijn

Community Member, 271 Posts

15 August 2009 at 3:50pm

Edited: 15/08/2009 3:51pm

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

Avatar
mc

Community Member, 5 Posts

16 August 2009 at 11:35pm

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?

Avatar
Martijn

Community Member, 271 Posts

17 August 2009 at 2:50am

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

Avatar
mc

Community Member, 5 Posts

17 August 2009 at 5:58am

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

}