In Twig 2, _self
seems to reference the name of the current template file.
Running {{ dump(_self) }}
from the file /templates/test/index.twig
would return the string test/index
.
So what it's being used for in this example...
{% if craft.app.request.segments|last == _self %}
... is to test wether the last segment in the URL matches the template file. Or "is this template being loaded directly from the file-based routing or is it being included from another template?"
I would point out tho, this only matches the last segment. It assumes your feed.rss
file it in the root templates directory. A more flexible approach would be to math the entire URI path.
{% if craft.app.request.pathInfo == _self %}
Later _self
is used to generate a link with {{ url(_self) }}
. This is the same as {{ url('feed.rss') }}
but would update if you changed the file name (e.g. news.rss).
The whole point of that conditional is to replace the {% include 'feed.rss' %}
with a <link rel="alternate" type="application/rss+xml" href="{{ url('feed.rss') }}">
. Which seems a little complicated. After all, it provoked this Stack Exchange question. ;)