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.

Template Questions

Display content if TotalItems > X


4 Posts   3417 Views


26 November 2009 at 6:45am Community Member, 70 Posts

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?




26 November 2009 at 7:24am (Last edited: 26 November 2009 7:26am), Community Member, 271 Posts

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();



26 November 2009 at 8:44am (Last edited: 26 November 2009 8:46am), Community Member, 604 Posts

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.


function EntriesMoreThan($value) {
   $num_entries = $this->DirectoryEntries()->Count();

if($num_entries > $value) {
   return true;
} else {
   return false;


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



9 December 2009 at 5:40am Community Member, 70 Posts

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?