I’d like to use a mailto
link in .../content/_footer.md
and I’d like the link to contain the name of the actual page being shown (as opposed to the footer file). How can I get this information?
-r
I’d like to use a mailto
link in .../content/_footer.md
and I’d like the link to contain the name of the actual page being shown (as opposed to the footer file). How can I get this information?
-r
You mean the page’s title as ‘name of actual page’? You get that with the .Title
page variable.
But I don’t really follow. Can you perhaps give an example of how ‘name of actual page’ looks like? Because I suppose you already thought of .Title
, so your use case is probably more complex and detailed than your question suggests.
OK, here’s an example. The title is “Bookshare” and the URL is “http://localhost:1313/at_cat/offering/computer/service/content/bookshare/”.
The subject for the mailto
should let me know where the user was, eg:
at_cat/offering/computer/service/content/bookshare/
FWIW, when I put Title: {{ .Title }}
in .../content/_footer.md
, the footer displays: Title: {{ .Title }}
, so I’m clearly pretty clueless…
-r
Ah I see. This is not something that Hugo can do. Because Hugo is a static site generator, it can ‘only’ generate HTML files. But Hugo can’t track or see which path people took to arrive at a certain page.
Luckily, JavaScript can with document.referrer.
That’s right, .Title
won’t do anything when you place them in content files. Such variables have to be put in the Hugo templates that render your theme or in a shortcode. Else Hugo can’t run them.
Although document.referrer
isn’t quite right for my use case (it returns the URL of the referring page), window.location.href
works nicely. Thanks!
Still, it seems to me that Hugo has to know the expected URL (or at least the pathname) of the current page while it’s processing _footer.md
. Isn’t this available?
-r
Not a partial called footer.html
? If it’s a partial, you can definitely get the title parameter…
Yeah, you can do that. @Jura was thinking of something more like analytics, where you can get referrer info from headers or whatever. But if you just want something like this:
<a href="mailto:rich@example.com?subject=Bookshare">Contact Rich</a>
To generate that, you do something like:
<a href="mailto:rich@example.com?subject={{ .Title }}">Contact Rich</a>
The partial @rdwatters mentions is where that will likely go, and you may need to run it through safeURL to get it working correctly. Make sure to check the docs on partials.
To close things out, here is the approach I’m currently using in _footer.md
:
Please feel free to <span id="email_link">foo</span>
comments, inquiries, suggestions, etc!
<script type="text/javascript">
document.getElementById("email_link").innerHTML =
'<a href="mailto:foo@bar.com?subject=' +
window.location.href + '">email</a>';
</script>