Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » include template based on nubmer of children

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

Page: 1
Go to End
Author Topic: 1064 Views
  • slith
    Avatar
    Community Member
    7 Posts

    include template based on nubmer of children Link to this post

    im trying to perform a conditional check to include a different template based on the total number of children. so if there is only one child, display the full entry...if there is more than 1, display a summary list.

    my code looks like this:

    // model //////////////////////////////////////////////////////

    class ExhibitionsHolder extends Page {

    ...

    public function numChildren(){

    return DB::query("SELECT COUNT(*) FROM SiteTree_Live WHERE ParentID = '$this->ID'")->value();

    }

    }

    // template code //////////////////////////////////////////////////////////

    <% if numChildren == 1 %>
    <!-- display only 1 entry -->
    <% include ExhibitionPage %>
    <% else %>
    <% if numChildren == 0 %>
    No entries found....
    <% else %>
    <!-- display summary of all entries -->
    <% include ExhibitionList %>
    <% end_if %>      
    <% end_if %>

    this works...however, wondering if there is a better way to do this without having to nest conditional statements.

    i tried using else_if...but it did not work.

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: include template based on nubmer of children Link to this post

    <% else_if Something %> is supported in SilverStripe, not sure if <% else_if Something == 0 %> is supported though. SSViewer is a very simple parser so most complex tasks are left to your PHP.

    If you wanted to clean that out you could rewrite your function to something which used a switch (or if .. else) statement in the PHP function and did a return $this->renderWith('TemplateName');

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