Page Variable for Base Filename?

Exploring using specific content file names to indicate the introduction and conclusion sections of a list collection in the _default templates. Is there an alternative to setting title? Was hoping to include conditional logic looking at the filename. I have something like the following now that works but requires title bet set and not just the filename:

{{ partial "top" . }} <div id="wrapper"> <main> {{ range where .Data.Pages "Title" "_" }} {{ .Render "intro" }} {{ end }} {{ range .Data.Pages }} {{ if not ( or ( eq .Title "_" ) ( eq .Title "__" ) ) }} {{ .Render "summary" }} {{ end }} {{ end }} {{ range where .Data.Pages "Title" "__" }} {{ .Render "outro" }} {{ end }} </main> </div> {{ partial "bottom" . }}

Just seems like there is a better way out there.

I don’t think so. But in my case, since .Title is easy to remember to just fill in with the page’s title, I’d put those underbar indicators in their own flags in frontmatter, and just keep .Title pure. Like “showintro” or “showoutro” for control of those sections.

On a tangent: I was trying to figure out how to incorporate the filename when I was experimenting with this:

… but I ended up using the slug, because I have the dates in my filenames, so they will sort, and I did not want to figure out how to strip those out.

Not sure what the thread starter wants, but there are page variables for file name, LogicalName, Dir, Ext, FullFilePath … Plenty.

Thanks for the responses. bep this is exactly what I was looking for.

Where is the best place to read about these? After about 20 minutes combing over the source not able to identify where the page variables you mention are included. Having difficulty identifying if they are native to the go template system or Hugo add-ons.

If folks agree they would be worth having in the Hugo docs, (which include other stuff that is already documented in the go template docs), then I’d be happy to write it.

Does anyone know of a dump technique for all the page variables? That would sure be the easiest way to see everything that is available.

Well after searching the site for page variables and seeing a previous thread going into the same frustration with just knowing the basic list I found the {{ printf "$#v" . }} trick and used it to isolate all the .File variables:

source.File{relpath:&#34;offering/_.html&#34;, logicalName:&#34;_.html&#34;, Contents:io.Reader(nil), section:&#34;offering&#34;, dir:&#34;offering/&#34;, ext:&#34;html&#34;, uniqueID:&#34;35cfcbc30c28b8b418f5d0e9aa5486aa&#34;}

FullFilePath simply doesn’t work.

LogicalName, Dir, and Ext do work but only when added to .File.

The tone and implication of your response is frustrating since none of these are documented anywhere and after learning some dark-magic trick to dump the template variables the best I get is a list of lower-case method names that happen to work and might not even be preferable to be used. The absence of documentation implies it was not intended to be used or is not baked enough to use reliably. Unfortunately this is the only place I can ask that question.

This is a big problem for Hugo currently. One I wish to help with.

Would it be worth adding a section to the Page Variables documentation for the File variables? If so I will submit a PR.

I realize I am new to Hugo and been posting a lot lately but I am motivated by a desire to not only use Hugo, but make Hugo a standard part of our Web course materials teaching it to all our students. The documents at the moment are far from suitable for sending students age 8 to 18 to use (and believe me these guys are very capable of figuring things out). In other words, I appreciate your cooperation and I will do whatever I can to further the documentation of Hugo in a way that is approachable to even the most novice blogger. Right now this is a major hurdle v.s. Harp, for example, which is less powerful but much more approachable. In fact, I almost dismissed Hugo out of hand because of this, but I’m glad I didn’t and want to help bring the bar of entry down. Is this part of the Hugo project mission or is it intended to keep high-level only?

I can say that our company is motivated enough to get behind Hugo that we are seriously thinking of forking our own branch permanently rather than the alternatives.

1 Like

This is up-to-date as to the latest source code:

The problem with this is that we have been a little bit sloppy with documentation on the methods, functions and types etc. … I’m doing some work in this area now, but there still is a long way to go.

Within a template, I want to get the filename (without extension) of a post / content file… my ugly but working solution for now is this:
{{ replace ( replace .File.LogicalName .File.Ext "" ) "." "" }}

{{ .File.BaseFilename }}

This is not working here, shows error

ERROR: 2016/05/26 template: post/li.html:4:105: executing “post/li.html” at <.File.BaseFilename>: BaseFilename is not a
field of struct type source.File in post/li.html

Using hugo v0.15 Windows 64bit (

Oops…typo. The “N” is capitalized.

{{ .File.BaseFileName }}

We’re working on documenting the .File fields. See PR#2155.

1 Like