Hello there,
I am looking to automatically set the title and the weight from the filename. Is such a feat possible?
For example, If I have filenames like follows;
100_My_Single_Page
110_My_Other_Single_Page
120_Yep_Another_Single
etc
I want the first file to have the following frontmatter somehow;
Title: My Single Page
Weight: 100
My motive for doing this is that I use hugo for writing documentation and I think by logic instead of alphabetised in my head. My editor orders all the files alphabetically, so I prefix all my files with numbers to allow me to visualise them to the same logical that is in my head. Does anyone have any suggestions?
Thank you kindly
John
The variable you are looking for is .File.BaseFileName
You can read here about Hugo’s File Variables.
Then you would need to pipe the above variable with the replaceRE
function to remove parts of the variable output with Regular Expression and store it in a different variable for use in your templates.
EDIT
Note that the above can only be used in Hugo Archetypes
Thank you kindly onedrawingperday. I will give that a shot and see what happens.
Would you suggest it would be better practice to perhaps use my own variables rather than attempting to overwrite the standard ones? For example. TitleByFilename and WeightByFilename …then just use those in the appropriate places?
Thank you
You cannot overwrite .File.BaseFileName
or any other Hugo template variable for that matter as far as I know.
You can define a new variable after performing Hugo functions against a template variable.
But the value of .File.BaseFileName
will always be that of the actual file name without the extension.
Name custom variables whatever you want but remember that the new variables will always have the dollar sign e.g.$WeightByFilename
1 Like
Thank you onedrawingperday, you have been most helpful. Really appreciate your thoughts and time. I have confidence this will do what I want…so off I go…
Thank you
John