Using .range in a single page and .md files

Hello!

I’ve been mostly configuring my site on my main .toml file and so far I thought I knew I could do anything with hugo, oh was I wrong.

I need to create a single page and the theme I am using was a one-pager so I am doing it from 0 but I have ran into a small issue with the .range thingy.

In the toml if one were to have some type of a list you would define it like this:

 [[params.offers.item]]
    title = ""
    subtitle = ""

And In code it would translate to something like this:

{{ with .Site.Params.offers }}

{{ range .item }}

{{ .title }}


{{ .description | markdownify }}

{{ end }}

Thus letting me add however many items in the toml file under the offers section.

This is exactly the same thing I want to replicate on my single page now but .md pages sadly dont have the same rules :c

In the page variables guide there is an example with the authors if you wanted to give names and so on I could use but I need the freedom of adding however many items I want.

How could I go by doing this?

First off you are missing a closing {{end}} tag for your with second I think you need to range .Params.offers.items and not item like you currently have, take a look at this post so you can see something similar: How can I make pairs of information in the front matter?

Uhm sure this would work inside my toml/config file but how would I do this in a .md file?

If I were to add

---
    [[testItem]]
        name = "hello"
---

Into the front matter or anywhere inside the .md I will get an error :

Error: Error building site: failed to parse page metadata for “first.md”: yaml: line 10: could not find expected ‘:’

How would I go by doing this in my .md/single-page file?

Okay I figured that what I am actually confused about is how can I create info pairs inside my config.toml file inside my .md-> what i presume is -> yaml file

You are using TOML syntax inside YAML front matter. Perhaps what you want is:

+++
[[testItem]]
  name = "hello"
+++

Same error, where should this be set in?

I replaced — with the +++ firstly but that gave me the same error and putting it under or inside the — gives the exact same outcome.

Are you trying this in your contents .md files right?

Share your site code, or at least your *.md file(s) and relevant template file(s), else this is hard to troubleshoot from afar.

I will add 2 with “toml?” and “yaml”

The first one with the yaml problem I understand that I am clearly using Toml parameters thus switching to +++ I encounter a different error, an error that I am not even sure what means.

If i go by some of the docs on here I concluded that they arent needed at all, then guess what no error but nor do I see any content If i ask for it, such as the h1->Title im calling out.

Notice that what I want to achieve isnt to Access Nested Fields in Front Matter that we can find in the docs BUT its somewhat close, I need to have multiple items and for the .html to add them accordingly just like you would do in a config.toml file

In the future, post your code in text format, not screenshots. See Requesting Help to learn how to do this.

So, your first.md still has invalid front matter. You changed the --- to +++, but did not convert the remaining variables to TOML syntax. It needs to be:

+++
title = "First"
date = "2018-11-03"
draft = false
[[testitem]]
  title = "A Title"
  description = "A Description"
+++

Also see the hugo docs on example configuration. They show the difference between YAML/TOML/JSON.

1 Like

Hazaa!! Ofcourse, this is what going through 2 docs 20 times for several hours does to you, an absence of eyes and critical thinking!

Thank you for your help!!

If you plan to use TOML for your front matter going forward, you may want to update your archetype file(s) so that new posts/pages are created with TOML.

1 Like