Forum

Thread tagged as: Question

Filter by category question

Hi

I'm using this to filter a perch_collection

perch_collection('Films',[
        'template' => 'list.html', 
        // Hardcode category path - how do I make this dynamic?
        'category' => 'film/buy/dvd/limited-edition/',
    ]);

The category value is fixed, at this point the template is filtering the collection by this path

film/buy/dvd/limited-edition/

How do I make anything after the set name dynamic?

I'm using routes:

film/[slug:buy]/[slug:format]/[slug:edition]
film/[slug:buy]/[slug:format]
film/[slug:buy]
film

Update

Adding this works

perch_collection('Films',[
        'template' => 'list.html', 
        // Dynamic path
       'category' => 'film/'.perch_get('buy').'/'.perch_get('format').'/'.perch_get('edition'),

    ]);
Stephen Meehan

Stephen Meehan 4 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Stephen,

If you are on /film/buy, you'd have 2 trailing slashes 'category' => 'film/buy// which I don't think works for filtering.

Try using something more robust. Perhaps something like this:

$catPath = false;

if(perch_get('buy')) $catPath = 'film/'.perch_get('buy').'/';
if(perch_get('format')) $catPath .= perch_get('format').'/';
if(perch_get('edition')) $catPath .= perch_get('edition').'/';

perch_collection('Films', [
'template' => 'list.html', 
'category' => $catPath,
]);

Hi Hussein,

Thanks for taking a look at this. I'm not seeing the problem with 2 trailing slashes.

It's working at the moment using the example I posted, however I do like the use of $catPath variable. Feels cleaner :)