<p>The easy way:</p>

<p><code>{% set relativeUrl = craft.app.request.url %}</code></p>

<p>Otherwise, and as a general example of using Craft helper functions...</p>

<p><code>rootRelativeUrl</code> is actually a method on the UrlHelper class, thus it's not available on elements I don't think.</p>

<p>Try this:</p>

<pre><code>{# Use any Craft "helper" classes in your Twig templates by creating them #}
{% set urlHelper = create(
    "craft\\helpers\\UrlHelper"
) %}
{# You can then call any of their methods, e.g.: #}
{{ set relative = urlHelper.rootRelativeUrl(entry.url) }}
</code></pre>

<p>(or you could get the url from <code>craft.request.url</code> for example)</p>

<p>(snippet pinched from from <a href="https://nystudio107.com/blog/cutting-the-cord-removing-plugins" rel="nofollow noreferrer">https://nystudio107.com/blog/cutting-the-cord-removing-plugins</a>)</p>

CC BY-SA 4.0
2020-04-10T16:22:51.490Z
Thanks! You put me on the right track. This worked: {% set relativeUrl = craft.app.request.url %}
Jason McKinney
CC BY-SA 4.0
2020-04-10T22:42:24.533Z
Well LOL yes, that's the easier way.  I got side tracked by the rootRelativeUrl thing.  Also I think in Craft 2 the request url was the full, not relative url...but I might be mis remembering.
Jeremy Daalder
CC BY-SA 4.0
2020-04-10T07:59:15.213Z
2020-04-10T22:43:16.127Z
2020-04-10T22:43:16.127Z
Jeremy Daalder
<p>I need to output the relative URL for the current page for a GA event label.
According to Craft 3 docs, it seems like rootRelativeUrl() as in: </p>

<pre><code>{% relative = entry.rootRelativeUrl() %} 
</code></pre>

<p>should do the job but it generates and 'Calling unknown method' error.
Any ideas?</p>

<templating>
Output Relative URL
# Output Relative URL

(Asked by: username_0 on 2020-04-10)

I need to output the relative URL for the current page for a GA event label.
According to Craft 3 docs, it seems like rootRelativeUrl() as in: 
{% relative = entry.rootRelativeUrl() %} 

should do the job but it generates and 'Calling unknown method' error.
Any ideas?

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2020-04-10. Score: 3 | Accepted answer

The easy way:
{% set relativeUrl = craft.app.request.url %}
Otherwise, and as a general example of using Craft helper functions...
rootRelativeUrl is actually a method on the UrlHelper class, thus it's not available on elements I don't think.
Try this:
{# Use any Craft "helper" classes in your Twig templates by creating them #}
{% set urlHelper = create(
    "craft\\helpers\\UrlHelper"
) %}
{# You can then call any of their methods, e.g.: #}
{{ set relative = urlHelper.rootRelativeUrl(entry.url) }}

(or you could get the url from craft.request.url for example)
(snippet pinched from from https://nystudio107.com/blog/cutting-the-cord-removing-plugins)

> **Comments:**
> **username_0**: Thanks! You put me on the right track. This worked: {% set relativeUrl = craft.app.request.url %}  
> **username_1**: Well LOL yes, that's the easier way.  I got side tracked by the rootRelativeUrl thing.  Also I think in Craft 2 the request url was the full, not relative url...but I might be mis remembering.  

---

