點燈坊

失くすものさえない今が強くなるチャンスよ

Using ref to Get Absolute Permalink

Sam Xiao's Avatar 2022-01-14

We can use ref to get the absolute permalink to a page for the href attribute of <a>.

Version

Hugo 0.91

ref

ref000

Absolute permalink of the about page.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="alpine.js" defer></script>
    <link rel="stylesheet" href="output.css" />
  </head>
  <body>
    <a href='{{ ref . "about" }}'>About</a>
  </body>
</html>

Line 10

<a href='{{ ref . "about" }}'>About</a>
  • ref : get the absolute permalink to a page
    • * : the context of the page from which to resolve relative paths, typically the current page (.)
    • about : the folder or file to get the absolute permalink

Conclusion

  • We have to use a double quote with String for Go template language, and the single quote is not allowed. So we have to use a single quote for the HTML attribute

Reference

Hugo, ref