Is it possible to get the slug as a variable?

An example use-case where I’d find this handy:

I am trying to work out a photo section on my personal website that stores individual albums as:

  • content/photos/slug-one.md
  • content/photos/another-slug.md

etc.

For each of these, I want to automatically refer to static files that serve as thumbnails for these in the list template. These files exist in a matching structure:

  • static/photos/slug-one.jpg
  • static/photos/another-slug.md

etc.

Right now, I am explicitly setting these in the front matter for each of the individual album markdown files, but if I could access the slug I could reduce this duplication.

1 Like

Perhaps a meta question that’ll help me resolve questions of this nature. Is it possible to see/output all variables available to a template?

See

http://gohugo.io/templates/debugging/

I don’t think that it is possible to output all available template variables automatically. Someone did this a while ago manually. The list should somehow up to date since it was created in februrary.

Furthermore, the docs don’t list the current slug as template variable. Maybe it’s not documented but implemented in the code base. @bep, do you know more?

If it’s not the case open an issue or hope for a temporary workaround.

Thank you!

Sorry, I didn’t mean to withdraw this post. I must have clicked something by accident.

I did it myself since @bep’s answer seemed to solve this problem already.

I have resolved this for myself, but just for anyone else stumbling upon this thread in the future:

There is indeed a slug exposed via {{ .Slug }}. Unfortunately, it is not auto-populated (e.g.from the name of the content file), and so needs to be explicitly set to be used. When it is set in the front matter it appears on the template and affects the permalink.

Good to know. But isn’t the slug internally available for every document?
I can change the permalink for content type in the following with:

[permalinks]
    posts = "blog/:year/:slug"

With this configuration it doesn’t matter a custom slug is defined in the frontmatter. The url or path to the file in the public folder will still be based on the filename.

This is what I assumed (and why I asked the original question). But I’ve been poking around using the debugging techniques @bep mentioned above and could not see it.