Pass variables to collection?

Hello!
Is there a way to pass variable to a collection ? Actually I made a snippet to fetch projects, with optionnal parameters, and I don’t really like having too much PHP and HTML on the template same file. So I tried with a collection, but it look like I can’t pass variable, by doing collection('projects', [$limit = 6] ? Or is there another way to do it properly ?

Thanks!

How about $collection->limit(6)?

Yes, that what I will use at the end, like on my current snippet. Theses variables are optional. See:

snippets/projects-list.php
<?php
/**
 * Project List Snippets
 * @param boolean|null $featured [Can include or exclude featured projects]
 * @param int|null $limit [Set a limit on the totals of projects to fetch]
 * @param boolean|null $shuffle [Shuffle the projects list]
 * @example [Example with using all parameters]
 * snippet('projects-list', [
 *   'limit' => 6,
 *   'featured' => true,
 *   'shuffle' => true,
 * ]);
 */

// Check if the projects page exist
if ($projectsPage = $site->find('projects')):

  // Look for child pages
  $projects = $projectsPage->children()->listed();

  if($projects->count()):
    // Look for featured projects
    if(!empty($featured)) {
      $projects = $projects->filterBy('featured', true);
    }

    // Set a limit on total of projects to fetch
    if(!empty($limit)) {
      $projects = $projects->limit(intval($limit));
    }

    // Shuffle the projects list
    if(!empty($shuffle)) {
      $projects = $projects->shuffle();
    }

    foreach ($projects as $project):
      dump($project);
    endforeach;

  endif;
endif;

My question is how to do that on a collection, with optional parameters too.

So your question is rather how to pass variables to the snippet, I guess? I don’t see where you are using a collection here? And in any case you cannot pass a parameter to the collection helper (not to a collection defined in a collection “template”.

So if you use a collection, you would do it like this:

$collection = collection('my-awesome-collection');
$limitedCollection = $collection->limit(5);

Hi,

My question is related to collection. I try to transform my current snippet to a collection.
So here it is with what i’ve started to do, a simple collection plugin declaration with optional parameters, like on my snippet:

plugins/custom-collections/index.php
<?php
Kirby::plugin('yoanmalie/custom-collections', [
  'collections' => [
    'projects' => function ($site, $featured = false, $limit = false, $shuffle = false) {
      $projects = $site->find('projects')->children()->listed();

      // Look for featured projects
      if(!empty($featured)) {
        $projects = $projects->filterBy('featured', true);
      }

      // Set a limit on total of projects to fetch
      if(!empty($limit)) {
        $projects = $projects->limit(intval($limit));
      }

      // Shuffle the projects list
      if(!empty($shuffle)) {
        $projects = $projects->shuffle();
      }

      return $projects;
    }
  ]
]);

I asked on how to add optional parameters here because I didn’t make it work by testing multiple thing. Since I currently manage theses optional parameters directly in my snippets, I thought it will be the same for collection… And it’s not possible, I have to simply use the chained methods like this:

$projects = collection('projects')->limit(5)->shuffle()->filterBy('featured', true);

Instead of what I’ve tried by reapplying my snippet:

$projects = collection('projects', ['limit' => 5, shuffle => 'true', featured=> "true"]);

(And cleaning unused stuff now in my collection plugin).

Thanks

As I mentioned above, it is not possible to pass additional arguments to a collection, no matter if is it defined in a plugin or in the collections folder.

If you want to pass all options in one go, you could, however, create a custom pages method.