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.

2 Answers 2

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)

2
  • Just tried and getting following error - When using set, you must have the same number of variables and assignments.
    – Mark H.
    Commented Mar 25, 2020 at 21:50
  • 1
    Brad, your edit did the trick. I just changed this part |split("-") to |split("-recipes"). Thanks.
    – Mark H.
    Commented Mar 25, 2020 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}}
1
  • 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.
    Commented Mar 25, 2020 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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