Get current page URL slug

Amit_95
Member | Platinum Partner

I want to get the slug from the current page. Do do this I'm using `{{ request.path }}`.

 

Currently, when executing `{{ request.path }}` it'll show me the slug as '/en/pro-test`. How can I get it so that it only shows me `/pro-test`?

0 Upvotes
4 Replies 4
jzilch
HubSpot Moderator

Hi, 

 

Using {{ request.path }} will return the entire path from the URL. I would try and use the split filter found in the HubL docs here. https://designers.hubspot.com/docs/hubl/hubl-supported-filters. To split on the '/' and then return just the part of the URL that you want to use. 

0 Upvotes
Amit_95
Member | Platinum Partner

So `{{ request.path|split('/') }}`?

 

With this approach, it prints `/en/[en,%20pro-test]`

0 Upvotes
jzilch
HubSpot Moderator

I would also set the second parameter of the split filter to display how many times it should split the URL. Also you will need to return the piece of the URL that you want to use. In this case you can use |last as the piece of the URL of the last item in your sequence. 

 

{{ request.path|split('/', 2)|last }}

Something like the above should work for you

0 Upvotes
Amit_95
Member | Platinum Partner

Ah, we're getting there!

 

That now spits out "pro-test". However, I have two translations of this page (English and French). I currently have ...

fr/{{ request.path|split('/', 2)|last }}

... added to the end of my domain, but it apends "?hsLang=en" to the URL (which results in a 404). Is there a way to remove the last bit too? It's not defined in my code so guessing it's HubSpot generated.

0 Upvotes