3214 Posts in 848 Topics by 810 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 894 Views |
-
include template based on nubmer of children

26 August 2010 at 1:06pm Last edited: 26 August 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.
-
Re: include template based on nubmer of children

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');
| 894 Views | ||
|
Page:
1
|
Go to Top |


