1

I'm using the following code for now:

Route (in the control panel)
    recipes/{slug} loads _recipes/restaurant

In the template to define the restaurant and list its items
    {% set restaurant = craft.entries.section('restaurants').slug(craft.app.request.getSegment(2)).one() %}

How do I change it to {slug}-recipes to make it work?

Advanced routing (in routing file) is preferred for performance.

Thanks.

1
+50

Route: {slug}-recipes

The in the template:

{% set parts = craft.app.request.getSegment(1)|split("-") %}
{% set newslug = parts[0] %}

Then you can use: .slug(newslug)

| improve this answer | |
  • Just tried and getting following error - When using set, you must have the same number of variables and assignments. – Mark H. Mar 25 at 21:50
  • 1
    Brad, your edit did the trick. I just changed this part |split("-") to |split("-recipes"). Thanks. – Mark H. Mar 25 at 23:47
1

I never do this before, but maybe it works.

return [
  '<value:{slug}>-recipes' =>  ['template' => '_recipes/restaurant'],
]

and you can get the value in a template like this :

{{value}}
| improve this answer | |
  • Yes, this approach would work according to the routing docs but it will slow the things down since I can't specify exact channel with entries to look for. – Mark H. Mar 25 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.