How to assign a specific template to article.md files?

I’m using someone’s template to build a hugo site. I’ve started with it and then I change it significantly. I think the author is a very advanced developer, but I can not understand some of his decisions. Thus, apart from having layouts/partials directory, he created layouts/_default directory and had put some templates there. The most used template is single.html, that renders all content/productdescription.md files.

The thing is, that besides content/productdescription.md files, I want to have some content/article.md files. My problem is, that all content/*.md files trigger launch of layouts/_default/single.html template - which is intended for products, not articles. Hence an error message immediately is thrown: “Price value is nil!” But articles are not supposed to have prices.

How I can assign content/article.md files to use not layouts/_default/single.html, but another template - e.g. layouts/partials/article.html? There should be some config somewhere - I can’t find it.

I tried using

---
title:
id: article
---

in frontmatter - but surprisingly it did not work.

The author uses other templates, but to make them work he uses a separate folder per each template and the data is loaded from .Site.Data folder. E.g. for FAQ article he uses layouts/faq/list.html template, and the data is loaded from /data/faq.yml file. Thus it requires a separate template per each article and creates too many folders.

I want to have my articles in content/articles.md files and have a single template for them in layouts/partials.
I would be very grateful if someone can tell me how to do that!

Here is an example:
At content/myarticle.md:

---
title: My Article
type: articles
---

At layouts/articles/single.html :

    <h1> Hello from article.html! </h1>

When you render everything you’ll see that line in public/myarticle/index.html

There are some rules that determine what template will be rendered where, which is worth reading. I just gave you one out of many possible answers, so find what is more convenient to you.

That being said, I believe that you’ll find this more convenient:

  • Save all the articles in the content/articles (content/articles/article1.md, content/articles/article2.md, etc)
  • Write the template in layouts/articles/single.html

The template is applied directly without having to specify type: articles in the front matter

No, it does not work. Error message:

Failed to render pages: render of "page" failed: "/.../layouts/_default/single.html:266:9": execute of template failed: template: _default/single.html:266:9: executing "main" at <index .Site.Data.prices .Params.supplier>: error calling index: value is nil; should be of type string
---
title: My Article
type: articles
---
/layouts/articles/list.html

The template should be at

/layouts/articles/single.html

instead of

/layouts/articles/list.html

Otherwise the default kicks in. Did that work?

Edit: The error is throwing is unrelated to the discussion I think. It says something about a .Params.supplier not being defined. I don’t know the internals of your project, so I don’t know exactly which parameters you need to define in your front matter to make it work.

Care to share the complete code, if it doesn’t have any sensitive information?

Thank you, I’ve found the solution.

1 Like