Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Display content if TotalItems > X

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

Page: 1
Go to End
Author Topic: 3403 Views
  • LesC
    Avatar
    Community Member
    70 Posts

    Display content if TotalItems > X Link to this post

    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

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Display content if TotalItems > X Link to this post

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

    }

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: Display content if TotalItems > X Link to this post

    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

  • LesC
    Avatar
    Community Member
    70 Posts

    Re: Display content if TotalItems > X Link to this post

    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

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