Archetypes/page variables not working on content files

Hi!

I’m in the process of rebuilding my personal site and I wanted to build a custom Hugo theme to go with it. Nothing fancy, but something that I could tweak pretty easily as I saw fit. However, I’m running into a bunch of odd issues.

  1. The archtypes/default.md file does not appear to be being applied whenever the hugo new notes/first.md command is run - the file metadata does not include the additional values that I’ve added to the default.md file
  2. Variables references from inside the first.md file (e.g. {{ .Title }}) are not rendering at all.
  3. There was a similar problem (although I think I have a somewhat passable workaround) where the examples I was seeing of referencing {{ range .Pages }} in the root index.html file were not working for me - I was able to get {{ range .Site.RegularPages }} working but that doesn’t seem correct.

Thanks in advance for any thoughts/ideas on what I could be doing wrong! And let me know if I can provide additional info/context.

I re-installed Hugo using snap install hugo and I’m running it on Ubuntu. Hugo version is Hugo Static Site Generator v0.74.3 linux/amd64 BuildDate: 2020-07-23T20:37:11Z.

Best,
John

The directory name should be “archetypes” not “archtypes”. You can create a site and theme scaffold with:

hugo new site mysite
cd mysite
hugo new theme mytheme

Markdown files (.md) are for markdown, template files (.html) are for HTML and templating code. {{ .Title }} is template code; it does not belong in a markdown file.

{{ range .Site.RegularPages }} in the root index.html template is typical, though you may want to filter using the where function.

If you have additional questions, please include a link to the public repository for your site. See:
https://discourse.gohugo.io/t/requesting-help/9132

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

Hi, @jmooring,

The “archtypes” was a typo on my end - it is “archetypes” in the repo.

Here is the temporary repo link.

The place that I have the {{ .Title }} references are in the front matter of the archetypes/notes.md file and in the _default/single.html file.

Let me know if I can provide further information!

Best,
John

Archetypes

First, you have both archetypes/notes.md and themes/notes/archetypes/notes.md. The first takes precedence. That’s how we can install a theme, and then override on file-by-file basis as needed without editing the original theme.

Second, I recommend creating and using a default archetype (default.md) instead of one tailored to your initial content type. You can add more archetypes later, if needed.

Third, this is wrong:

---
title: "{{ .Title }}"
date: {{ .Date.Format "2006-01-02" }}
draft: true
tags: [""]
---
  1. {{ .Title }} returns the value assigned to the title variable in frontmatter; your logic is circular.
  2. If you want to set date to YYYY-MM-DD, use {{ dateFormat "2006-01-02" .Date }}. But don’t do that. I think you’ll regret it later. Instead, set the date to a fully qualified date string, including UTC offset. You will format the display of the date in your templates. If I write three posts on the same day, how will I sort them without knowing the time at which they were written?

So, do this instead:

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
tags: [""]
---

config.toml

There’s no need to do this:

contentdir = "content"
layoutdir = "layouts"

These are the defaults. Including defaults in the config file may be useful while learning, but it makes the config file more difficult to read as it grows.

Layouts

Read about baseof.html and blocks. It will make your life easier.

I’ve added in those fixes/various cleanup as per your recommendation, @jmooring and I’ll take a look at the baseof.html stuff as well.

One thing remaining is that variables are still not being read in the note.html partial on the newly generated content files (e.g. fouth.md). The {{ .Title }} is still empty, for example.

Sorry I missed that. Instead of this:

{{ partial "foo.html" }}

Do this:

{{ partial "foo.html" . }}

The dot represents the current scope, and it must be passed along to the partial.

References:

One of the most common mistakes with new Hugo users is failing to pass a context to the partial call. In the pattern above, note how “the dot” ( . ) is required as the second argument to give the partial context. You can read more about “the dot” in the Hugo templating introduction.

1 Like

Awesome, that did it - thank you very much! I’ve marked that response as the solution but the one above is also a solution to the other questions for anyone else coming across this thread in the future.

Thanks for sharing the repository. It make troubleshooting far more efficient, and reduces time-to-resolution.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.