Collections
Mapping
You can map over your collection items by adding a map
key to the collection's array in config.php
, and specifying a callback that accepts the collection item. Each item is an instance of the TightenCo\Jigsaw\Collection\CollectionItem
class, from which you can instantiate your own custom class using the static fromItem()
method. Your custom class can include helper methods that might be too complex for storing in your config.php
array.
config.php
1<?php 2 3return [ 4 'collections' => [ 5 'posts' => [ 6 'map' => function ($post) { 7 return Post::fromItem($post); 8 } 9 ],10 ],11];
Your custom Post
class should extend TightenCo\Jigsaw\Collection\CollectionItem
, and could include helper functions, reference and/or modify page variables, etc.:
1<?php 2 3use TightenCo\Jigsaw\Collection\CollectionItem; 4 5class Post extends CollectionItem 6{ 7 public function getAuthorNames() 8 { 9 return implode(', ', $this->author);10 }11}