<p>In Twig 2, <code>_self</code> seems to reference the name of the current template file.</p>

<p>Running <code>{{ dump(_self) }}</code> from the file <code>/templates/test/index.twig</code> would return the string <code>test/index</code>.</p>

<p>So what it's being used for in this example...</p>

<pre><code>{% if craft.app.request.segments|last == _self %}
</code></pre>

<p>... 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?"</p>

<p>I would point out tho, this only matches the last segment. It assumes your <code>feed.rss</code> file it in the root templates directory. A more flexible approach would be to math the entire URI path.</p>

<pre><code>{% if craft.app.request.pathInfo == _self %}
</code></pre>

<p>Later <code>_self</code> is used to generate a link with <code>{{ url(_self) }}</code>. This is the same as <code>{{ url('feed.rss') }}</code> but would update if you changed the file name (e.g. news.rss).</p>

<p>The whole point of that conditional is to replace the <code>{% include 'feed.rss' %}</code> with a <code>&lt;link rel="alternate" type="application/rss+xml" href="{{ url('feed.rss') }}"&gt;</code>. Which seems a little complicated. After all, it provoked this Stack Exchange question. ;)</p>

CC BY-SA 4.0
2019-09-10T18:58:45.797Z
Thanks a lot for your explanation. everything is clear now :)
hvs
CC BY-SA 4.0
2019-09-10T06:03:41.493Z
2019-09-10T06:03:41.493Z
Alex Roper
<p>While reading this blog article about <a href="http://craftsnippets.com/articles/rss-feed-template-component-for-craft-cms" rel="nofollow noreferrer">RSS templating</a>, I came across the following twig code</p>

<pre><code>if craft.app.request.segments|last == _self
</code></pre>

<p>I understand that the above command, in general, yields URL components as an array. But I don't understand under what scenario is the above condition true. What does <code>_self</code> mean in this context?</p>

<templating>
What is the significance of craft.app.request.segments|last == _self
# What is the significance of craft.app.request.segments|last == _self

(Asked by: username_0 on 2019-09-09)

While reading this blog article about RSS templating, I came across the following twig code
if craft.app.request.segments|last == _self

I understand that the above command, in general, yields URL components as an array. But I don't understand under what scenario is the above condition true. What does _self mean in this context?

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2019-09-10. Score: 2 | Accepted answer

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 . Which seems a little complicated. After all, it provoked this Stack Exchange question. ;)

> **Comments:**
> **username_0**: Thanks a lot for your explanation. everything is clear now :)  

---

