Forum

Thread tagged as: Question, Problem, Runway

Collections filtered count

Hello,

I have a collection called Classes. Below is its template with fields for a course title, category, and staff selection. Categories are general subjects like English, Mathematics, Science, etc. The related Staff collection template is basically a brief profile for each staff member.

<perch:content id="classsubj" type="text" label="Course Title" required="true" title="true" />

<perch:categories id="classcat" label="Category" set="academics" />

<perch:related id="classstaff" collection="Staff" label="Staff">
    <perch:content id="name" type="text" />
</perch:related>

I'm able to display a full listing of all classes with their corresponding staff and categories on my page using:

perch_collection('Classes', [
    'template'   => '_class_list_display.html',
    'sort'          => 'classsubj',
    'sort-order' => 'ASC'
]);

Here's my _class_list_display.html template:

<perch:before>
    <div class="table-responsive">
        <table class="table table-hover">
        <thead>
            <tr>
                <th>Class</th>
                <th>Instructor</th>
                <th>Department</th>
            </tr>
        </thead>
        <tbody>
</perch:before>

        <tr>    
            <td>
                <perch:if different="classsubj">
                    <a href="classes/<perch:related id="classstaff" collection="Staff" scope-parent="true"><perch:content id="nslug" type="slug" /></perch:related>/<perch:content id="classslug" />" title="Visit course page"><perch:content id="classsubj" type="text" /></a>
                </perch:if>
            </td>
            <td>
                <perch:related id="classstaff" collection="Staff" scope-parent="true">  
                    <a href="classes/<perch:content id="nslug" type="slug" />" title="Visit profile page"><perch:content id="prefix"/>. <perch:content id="Name" /></a>
                </perch:related>
            </td>
            <td>
                <perch:categories id="classacad" label="Category" set="academics">
                    <a href="" title="Visit department page"><perch:category id="catTitle" /></a><br/>
                </perch:categories>
            </td>
        </tr>

<perch:after>
        </tbody>
        </table>
    </div>
</perch:after>

What I want to do is provide a filtering list in my sidebar that's similar to the way Categories is listed in blog, as in this blog template:

<perch:before>
<h2>Categories</h2>
<ul>
</perch:before>
      <li><a href="/news/category/<perch:category id="catSlug" />"><perch:category id="catTitle" /> (<perch:category id="count.blog.post" when-empty="0" />)</a></li>
<perch:after>
</ul>
</perch:after>

But I'm not sure and getting stuck trying to accomplish this.

Here's the template (filterstaff.html) that I'm using to generate a list of the staff members that have been selected in the Classes collection:

<perch:related id="classstaff" collection="Staff" scope-parent="true">  
    <a href="?inst=<perch:content id="nslug" type="slug" />" title="Visit profile page"><perch:content id="name" /></a><br/>
</perch:related>

When I output to my page using:

perch_collection('Classes', [
      'template' => 'classrooms/_filterstaff.html'
]);

I get a list of the staff members, but I get each instance of their name selected so:

  • John Smith
  • Sue Johnson
  • John Smith
  • Michael Smith
  • John Smith
  • Michael Smith

...and so on. What I want to accomplish is something like:

  • John Smith (3)
  • Sue Johnson (1)
  • Michael Smith (2)

So what I'm aiming for is a single instance of each name and a total count of instances. I see the categories template for blog uses <perch:category id="count.blog.post" when-empty="0" /> to output a count of that category. How would I do this for a collection where I want a count of the collection items where a staff member is selected?

Any help or direction is appreciated.

Joshua Rodriguez

Joshua Rodriguez 2 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I can't think of a way to do that within a template. You could fetch the data back with skip-template and count it, perhaps.