Global public data

Requirements: There are a variety of products on my page. The prices of the products may change frequently. Multiple different pages use the product price information.
Question: I want to know if there are global variables or constant attributes that can be used in the md file in the content directory? The reason I don’t like to use shortcodes is that the structure and style of my page display prices are always inconsistent, so using shortcodes will be troublesome. The reason for not using the data directory is that I often have multiple products sharing the same HTML, and I don’t want to write logical judgments in the HTML, so I would like to be able to use a global constant in the md file corresponding to the product for management, but It seems that no way has been found

Are you sure that a static site generator is the right tool, then? Typically, that’s where a database and dynamically generated pages come into play. With Hugo, you’d have to

  • either regenerate the site after each price change, or
  • provide JavaScript that loads the current prices from somewhere at run time.

Yes, I use hugo to generate the page. I would like to know if there are global variables or constants that can be used in the md file.

You can’t access front matter or site parameters from MD files directly. Use shortcodes.

But those don’t do anything to alleviate the original problem: You want to manage dynamic content with a static site generator. That’s a bit like using a screwdriver to drive in a nail – feasible, but not the best choice.

I’m not sure what you mean by that. You can write markdown like this:

You can purchase Product 1 for **{{< p 1 >}}** at any hardware store.

Foo|{{< p 1 >}}
Bar|{{< p 2 >}}
Baz|{{< p 3 >}}

To get this:


All driven from a data file like this:

- id: 1
  name: Product 1
  description: This is Product 1.
  price: 67.00
- id: 2
  name: Product 2
  description: This is Product 2.
  price: 42.00
- id: 3
  name: Product 3
  description: This is Product 3.
  price: 67.42

I have tried that this is feasible, but I actually want to use it in the template file in the content directory to achieve the purpose of rendering this variable in html instead of directly using the md format for rendering.

I don’t know what this means. Template files go in the layouts directory, not the content directory.

Yes, what I mean is that I want to render it into a file under layout and give it different styles, but looking at the example you gave, it seems that I can’t do it.

Just use a partial instead of a shortcode.

I may understand, what you mean is that I render the price in the partial, and then specify the rendering of the partial in the file. In this way, all the price control is placed in this partial, right?

Yes, but it’s more verbose.

{{ partial "p.html" 2 }}

With a data/pricebook.yaml file like this:

  name: Product 1
  description: This is Product 1.
  price: 67.00
  name: Product 2
  description: This is Product 2.
  price: 42.00

You could do this at the top of the template:

{{ $pb := site.Data.pricebook }}

Then render price by key:

{{ $pb.p2.price }} --> 42

Note that the key in the data file must begin with a letter or underscore in order to use the dot-chaining syntax.

Okay, I probably have some ideas, thank you very much