Set frontmatter variables from templates? How?


#1

Hello there,

I am attempting to get HUGO to output a glossary. I have a situation where, I am wanting to set frontmatter variables according to a filename (rather than having to set it manually). For example;

I have the following directory structure;
\Glossary
\Glossary\a
\Glossary\a\aardvarrk.md
\Glossary\a\apple.md
\Glossary\b
\Glossary\c
\Glossary\etc

My frontmatter variables might look like this;

title: “aardvark”
categories: a
type: glossary

My list.html template (for showing the glossary) looks like this;

<main>
<div class="container">
	<div class="glossary">
		<h1>Welcome to the glossary</h1>
		<h3>Learn time!</h3>
		<div class="footerbar">
			{{ if isset .Site.Taxonomies "categories" }}
			{{ if not (eq (len .Site.Taxonomies.categories) 0) }}
			<div class="categories">
		        <ul >
		        	<li><a href="/glossary">All</a></li>
		            {{ range $name, $items := .Site.Taxonomies.categories }}
		            <li><a href="/categories/{{ $name | urlize | lower }}">{{ $name }}</a>
		            </li>
		            {{ end }}
		        </ul>
			</div>
			{{ end }}
			{{ end }}
			{{ range (where .Data.Pages "Type" "glossary") }}
				<a href="{{ .Permalink }}">{{ .Title }}</a> 
			{{end}}
		</div>
	</div>
    </div>
</main>

My goal is so that I do not need to set the categories front matter manually (ideally the title either). I want to read it from the filename? Is such a thing possible?

Thank you for any thoughts
John


#2

Your best bet is to automate it with a script (e.g. python) that manipulates the content file contents (frontmatter variables) accordingly.


#3

Do you mean preprocess it? before hugo.exe gets to it?


#4

Yes, editing the file with your custom script. If it’s auto-building like on dev environment, it will detect after modification.


#5

Thanks pricestamp. It’s doable. I’d rather not have to do it that way if I dont have to. Can you (or anyone) think of a way that it can be done without preprocessing? (I know hugo is still pretty new, but I am sure I am not the only one who would want to achieve similar feats?)

Thank you kindly for any thoughts
John


#6

Related?


#7

Another option is to adapt this script to your use case:


#8

Thanks zwbetz…

Actually, that post is exactly what I am trying to do in another place of my website also…(was hopeing the answer to this question would lead me also to the answer for that question :slight_smile: )

I was hoping not to have to preprocess things (it increases complexity)…but it is beginning to look like that’s the only way to do it…I’m in a windows environment so I’m going to have to adapt that script to something else, but it’ll work for sure…

I wish it was simpler…

Thank you kindly for your help, much appreciate it…


#9

No prob. Also, if you use Git Bash for Windows, you should be able to use that script as-is (since Git Bash includes awk and many other UNIX-like utilities)


#10

I use an WIn batch to generate my post directories.

@echo off
@set /p id=Enter Title: 
@set temp=%DATE:.=%
@set title=%id: =_%
@set dirname=%temp:~4,4%-%temp:~2,2%-%temp:~0,2%_%title%
@set jahr=%temp:~4,4%
@start /B hugo new post\%jahr%\%dirname%\index.md 
@ping -n 3 localhost> nul
@mkdir content\post\%jahr%\%dirname%\gallery;
@mkdir content\post\%jahr%\%dirname%\images;

It is in the same directory of hugo.exe , I named it newpost.bat
If it helps, take my part and modify it for your goals.