Forum

Thread tagged as: Question, Configuration, Runway

Collection in Runway

Hi,

Can i clarify is it possible to have a master template output an item from a collection which matches the URL

I have created a "departments" collection and also 4 pages which share the same master template. However i wish that based on the department you choose from my main nav the page will display only the relevant department from the collection.

Does this make sense, possible? I can see you can sort by cat so should i add them to there own cat's and then can i match the cat to say the slug of the heading?

cheers

Steve Forbes

Steve Forbes 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you'd do something like:

if (perch_get('dept')){
    // show one department
    perch_collection('Departments', [
        'filter' => 'slug',
        'value' => perch_get('dept'),
    ]);
}else{
   // list all departments
   perch_collection('Departments');
}

And you'd add a route like

departments/[slug:dept]

to be able to have a URL like

/departments/human-resources

cheers drew, Runway is superb!!

Could i have found this in the docs? i spent a fair bit of time head scratching.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Not as a packaged solution, but all the component parts are there.

Sorry drew,

Does the route do away with needing to append the slug to the url?

To confirm i understand the above to match the slug in the string to my slug "dept" in my template.

In my mind i'm trying to use the named segments i believe, so my url controls the item from the collection.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to have the slug in the URL, yes.

got it,

i had

'filter' => 'slug'

rather than

'filter' => 'dept'

i can browse to /departments/human-resources fine and get the exact collection match from my collections. But am i able to pass the slug in the navigation which is generated or route in simply that mydomain.com/human-resources routes to /departments/human-resources. Im guessing i cant just drop /departments from the route so its just [slug:dept]

thanks in advance

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can do that, but how will you then know which department to display?

So the slugs cant come from the first level?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, they can be anywhere, as long as you can identify them in the URL and not accidentally match unrelated pages.

Ok, i'm guessing its not as simple as crating a route which is,

/[slug:dept]

rather than

/departments/[slug:dept]

as i've tried this and it doesn't match the route. I am trying to achieve it so that i can have mydomain.com/human-resources with the human-resources being the slug and there for being passed to my if statement via dept, which then matches the slug of the heading in my template. This all works if i route via /departments/[slug:dept] but i'd like to route direct? Im not sure how to achieve this.

Drew McLellan

Drew McLellan 2638 points
Perch Support

A route of [slug:dept] would match almost every page in your site, wouldn't it?

yes i guess but the if statement in the page template would determine the output based on the slug wouldn't it?

I can modify my navigation item template to have the /departments/<perch:pages id="pagePath" /> and its not an issue. I just wanted to know if i was going about things the right way?

Drew McLellan

Drew McLellan 2638 points
Perch Support

There isn't really a right on wrong way - only what works or doesn't work for the site you're building. What you're doing sounds fine.

Drew, thanks for your patients and advice.

Be good to see some routing examples/solutions if you have any plans to add any in the future. Get a feel for other methods.

Hi Drew,

Ive got to the stage now where i have setup my departments master page and under this i have several pages for each department. Each department shares the same master template and i am using the above code to decide upon which item from the departments collection to display on the page.

Now i now wish to add an additional page(department) under the departments however this page must display differently and use a different template. which is where i see an issue with my routes.

Each department page has the route of departments/[slug:dept] this is ok whilst they share the same template:

departments/hr departments/admin departments/accounts

but now i wish to have departments/technical but with a different template. I added the page as normal, and selected the different master template but the routes already in place for the above pages mean it uses there template.

Im struggling to grasp the routes. Can you point me in the right direction?

Drew McLellan

Drew McLellan 2638 points
Perch Support

The order of the routes matters. Put your departments/technical route before the departments/[slug:dept] route in the routes list.

Tried this, It works in that it now matches the route departments/technical and uses the correct template but i don't have the slug to filter the content.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so you can add a name like you've done in the other route:

departments/[technical:dept]

i've worked round it by being more specific in my additional template, and dropping the if statement. But ill give this a go. Cheers