0

I'm building a coursework site and have a Course section, who's entries will be courses containing work that the client will assign to users. I built in both the work itself (different "lessons" sections), and fields for the info page that will be displayed to all users of the site.

How can I use two different templates with the same entry? I need to be able to link to the course.url for the info page, as well as the course.url for the work page within the different templates that use info from the courses.

Currently I have all of the course entries urls going to the info page template. Do I need to create a separate section for course work?

  • Do you know twigs {% extends %} and {% include %} and {% block %} tags? These do exactly what you want – Robin Schambach Mar 8 '18 at 18:52
1

You do not need to create a separate section for course work.

What you want to have is two different urls, that both display the same entry, but in two different ways, with two different templates. So for example /course-slug will be the url for info page for the entry with slug course-slug, and /course-slug/lesson will be the url for the work page for the entry.

If you think of this as two different routes, you have set one of those routes up in the course section. You can set up a second one as a dynamic route. You want to route [slug]/lesson (uri) to _work (template) (or whatever your template is). Then in the _work.twig template, you will need to fetch the entry from the slug:

{% set entry = craft.entries.section('course').slug(slug).first() %}

Check if no entry is set, and if not {% exit 404 %}. And then proceed as normal.

  • This worked exactly the way I needed. I hadn't checked out dynamic routes yet. Thank you! – FuzzyJelly Mar 8 '18 at 20:31

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.