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 /

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

include template based on nubmer of children

Go to End

2 Posts   1343 Views


Community Member, 7 Posts

26 August 2010 at 1:06pm

Edited: 26/08/2010 1:09pm

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.


Forum Moderator, 5521 Posts

26 August 2010 at 9:11pm

<% 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');