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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Display content if TotalItems > X


Go to End


4 Posts   5028 Views

Avatar
LesC

Community Member, 70 Posts

26 November 2009 at 6:45am

I need to show a block of content if the total items for a control is greater than a certain number. I know that if I output $getDirectoryEntries.TotalItems it returns the right value.

I hoped the following would work:

<% if getDirectoryEntries.TotalItems > 10 %>Bla bla<% end_if %>

But then I found out that the template engine doesn't support greater than or less than comparators.

I then tried the following:

<% if showItemsControl(getDirectoryEntries.TotalItems) %>Bla bla<% end_if %>

function showItemsControl($totalitems) {
		if ($totalitems > 10) {
			return true;
		} else {
			return false;
		}
	}

But that throws the same error (syntax error, unexpected '}'). Is this because of the order of rendering the templates?

Can anyone shed a little light on how to do this?

Cheers

Les

Avatar
Martijn

Community Member, 271 Posts

26 November 2009 at 7:24am

Edited: 26/11/2009 7:26am

And if you try it with curly braces?:

<% if showItemsControl({getDirectoryEntries.TotalItems}) %>Bla bla<% end_if %>

You could also write a seperate function in your Object to return the total items, something like (not tested):


class DirectoryEntrie extends DataObject{

function getTotalDirectoryEntries() {
    $sqlQuery = new SQLQuery(
      "COUNT(Item.ID)", //select
      "Item", //from
      "Item.DirectoryEntrieID =".$this->ID //where

    );
    return $sqlQuery->execute()->value();

}

Avatar
Double-A-Ron

Community Member, 607 Posts

26 November 2009 at 8:44am

Edited: 26/11/2009 8:46am

AFAIK, templates still do not allow you to pass template vars as function params like that.

When you think about it, when the system parses your template, it wouldn't know whether to parse the function call or the variable value first, so this sort of functionality wouldn't be easy to add to a templating engine.

Marti's suggestion is the best way of doing this.

Controller

function EntriesMoreThan($value) {
	$num_entries = $this->DirectoryEntries()->Count();
    
    if($num_entries > $value) {
    	return true;
    } else {
    	return false;
    }
}

Template

<% if EntriesMoreThan(10) %>

My function example may not work for you as I was unsure what the structure or scope of your object was. Add the $value param gives you a little flexibility with the method so you don't find yourself writing a new one just to check for 20/50/100 if the need ever arises.

Aaron

Avatar
LesC

Community Member, 70 Posts

9 December 2009 at 5:40am

Excellent, thanks for the help guys.

Can you tell me if the $this->DirectoryEntries()->Count(); method can accept filters? So I can count the number that meet a certain condition?

Cheers