Liquid markup to detect current page URL?
Asked Answered
P

2

28

I've just recently started using Github to host my blog (using Jekyll and Liquid). However, I'm having an issue which I can't currently fix. The issue could be hacked/solved if I was able to detect which "page" or "url" the user was visiting.

Something like:

{% if user_is_currently_at_this_url %}
    {{ display something }}
{% else %}
    {{ display something else }}
{% endif %}

Is this possible? Is there any other way around this issue?

Palmary answered 23/3, 2012 at 3:5 Comment(1)
page.url or {{ page.url | absolute_url }} in case you want to get absolute URLAlexandros
T
47

page.url is the URL of the current page, without the host (e.g. /index.html), as documented in Page Variables. So, in this case:

{% if page.url == "/index.html" %}
   something
{% else %}
   other thing
{% endif %}

(However, I don't think you need this any more, your other problem is probably solved. :) )

Toul answered 23/3, 2012 at 3:23 Comment(6)
Yes to my problem being already solved, but thank you for this :)Palmary
page.path might be better as illustrated at github.com/jekyll/jekyll-help/issues/5#issuecomment-39033862Larine
This doesn't work for 404 redirects on github pages :(Subjoinder
@LegoStormtroopr did you happen to find any solution for that?Hillaryhillbilly
Sadly, no I did not :(Subjoinder
This might not always return the browser's current URL. In the case of a 404, for example, the user's browser could be pointed to one URL (site.com/page/that/does/not/exist) but the rendered page's URL or path could be different (404.html, say)Diverticulitis
I
4

You could use {{ canonical_url }}

Inbreathe answered 28/3, 2022 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.