I'm using Craft 3, and I'm struggling to make sense of how to address the following. (I've not had need to venture into a third URL segment before.)
I have a url of the form:
stories/{slug}
served by a template:
stories/_entry
I'm aware that the 'entry' variable gets set automatically. So far so basic.
But I also want to address urls of the form:
stories/{slug}/{variable}
…where variable
is used to fetch content from a specific Matrix row in the entry that matches slug
.
It is important that variable
forms a third segment addressable as its own page, and not just a URL query parameter.
I have tried 2 approaches:
- Getting segment 3 and then using it in a conditional inside of
stories/_entry
:
{% set seg3 = craft.app.request.getSegment(3) %}
{% if seg3|length %}
{% include 'partials/extract' %}
{% else %}
{% include 'partials/story' %}
{% endif %}
…but when seg3 is not empty Craft says:
Template not found
- Setting a route to a different template:
stories/{slug}/{*}
…is processed by:
stories/_extract
…in which, as a basic test I have:
{% block meta %}
<title>{{ entry.title }}</title>
{% endblock %}
…but Craft then reports a Twig runtime error, saying:
Variable "entry" does not exist
So I tried using:
{% set seg2 = craft.app.request.getSegment(2) %}
{% for entry in craft.entries.section('stories').slug(seg2).all() %}
{% block meta %}
<title>{{ entry.title }}</title>
{% endblock %}
{% endfor %}
…but that makes no difference.
Is my approach 2 definition of entry
wrong?
Why does a non-empty value in segment 3 cause approach 1 to fail?
I'm obviously missing something fundamental here. Any pointers much appreciated.