Count checked checkboxes

This might be a last day in the office before Christmas issue, but I can’t work this out. How can I count how many checkboxes have been checked in a checkboxes panel field, and output this on the site? The data is comma separated in the content file, looks like this:

Courses: page://ysi7jy5Ob9lIN3GX, page://ysi7jy5Ob9lIN3GX

Checkbox setup looks like this:

options:
  type: query
  query: site.index.filterBy('intendedTemplate','course')
  value: "{{ page.uuid }}"

If someone could give me a quick pointer that would be great. I will need to use these values in a filter eventually too, but just a count would be great for now

First you want to turn the field value into an array, or, even better, a pages collection.

You could use $page->courses()->toPages(',') (untested):

$courses = $page->courses()->toPages(',');
if($courses) {
  echo $courses->count();
}

I was being an idiot - switched it for a pages field which made things a lot simpler. Thanks for replying

Just for completeness’s sake: The if statement here is redundant, because it will always return true.

While toPage() for a single page returns null or a page object, toPages() always returns a collection even if without data.