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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

[SOLVED] - custom count in loop


Go to End


4 Posts   3358 Views

Avatar
pouic

Community Member, 19 Posts

6 November 2014 at 5:12am

How to determinate first element in childrens loop when className is XPTO

Using the $Pos not a solution because the first children can not be the Page with className XPTO.

<% loop $Children %>
            <% if ClassName == XPTO %>
                <li class="col-xs-2 <% if <b>$CustomCount == 1</b> %>active<% end_if %>"><a data-toggle="tab" href="#tab-$ID">$Title</a></li>
            <% else %>
                <li class="col-xs-4">$Title</li>
            <% end_if %>
<% end_loop %>

Avatar
zenmonkey

Community Member, 545 Posts

6 November 2014 at 5:38pm

Edited: 06/11/2014 5:39pm

Since Pages always return in SiteTree order what about creating another function that finds the XPTO then compare the ID

So in your page controller;

public function FirstXPTO() {
  $first = XPTO::get()->First();
  
  if ($first) {
    return $first->ID;
  } else {
    return 0
  }
}

Then in template:

<% loop $Children %> 
<% if ClassName == XPTO %> 
<li class="col-xs-2 <% if $ID == $Up. FirstXPTO %>active<% end_if %>"><a data-toggle="tab" href="#tab-$ID">$Title</a></li> 
<% else %> 
<li class="col-xs-4">$Title</li> 
<% end_if %> 
<% end_loop %>

You'll need to drill up to the top controller with more $Up(s) or $Top depending on the Depth of the loop. You could probably also simplify the FirstXPO function to just retun the ID using SQLQuery instead of the whole objcet, but you get the idea. The other option is to add a method to the XPTO class to determine if its the first one. Either the XPTO xlass needs to know if it s first or the main page needs to know which is XPTO is first

Avatar
pouic

Community Member, 19 Posts

6 November 2014 at 10:09pm

Thak's for your suggestion.

Using same logic and $pos variable the other solution is

On controller:

   function getActiveTab()
    {
        $i = 0;
        foreach ($this->Children() as $child)
        {
            $i ++;
            if ($child->ClassName == 'VotingPage'){
                return $i;
            }
        }
    }

in template:

<% loop $Children %>
<% if ClassName == XPTO %>
<li class="col-xs-2 <% if <b>$getActiveTab == $pos</b> %>active<% end_if %>"><a data-toggle="tab" href="#tab-$ID">$Title</a></li>
<% else %>
<li class="col-xs-4">$Title</li>
<% end_if %>
<% end_loop %>

Avatar
zenmonkey

Community Member, 545 Posts

7 November 2014 at 3:45am

True that works, though I'd be careful it may impact performance depending on how complex your DataObjects are since you're essentailly pulling the Children() multiple times. Once in the tempalte the and once in the getActiveTab function. Getting one XPTO record may be less overhead, I'm not sure if the Children() query is cached for the whole controller or just teh temaplte function.