Hello! I want to store my product’s informations within JSON-file (like product name, product price, release date, etc).
Because those products will display in different places (articles, pages, related content and other stuff). And for me convenient to store necessary information within single json, and add/edit new products within one single json file.
But I have a question. Is it possible then to extract and show data from my json file within markdown posts? I’m looking the documentation: Data Templates | Hugo, but can’t find any information about extracting and rendering data within markdown files.
No, I don’t want to display product on every post (then I shouldn’t to place site.Data within layout template).
But I want to use product info from site.Data within one specific post (markdown). How I can render site.Data within specific markdown file? For example, I have an article about best keyboards for gaming . Then I want to here extract some keyboards from my Data (JSON) file.
Please see configuration parameter enableInlineShortcodes on Create your own shortcodes | Hugo .
Before you set it to true: The security hint there is NOT for nothing!
I have a keyboard in my data file. An example data:
Name: ASUS ROG Claymore II
Price: $229
Link: link-to-the-site.
And within different posts around text to promote this keyboard will be different. For example:
Post 1. We recommend you the keyboard: HERE IS KEYBOARD NAME and price is KEYBOARD PRICE
Post 2. Our recommended keyboard is KEYBOARD NAME, the actual price is equal to KEYBOARD PRICE.
I mean, I want to display name, price in different places within my post. Also textaround always should be different (to show keyboard name, keyboard price, link to buy the keyboard, etc). Sometimes I want to display only keyboard name, or keyboard price, or them both.
Sorry for bad English, I believe I was able to explain it.
Thank you for the answer! But I don’t want to use a shortcodes, in my case it doesn’t help. I need to directly extract a data files within my content files (markdown posts).
This might be the best solution for your case. Here is an example I use with frontmatter variables in shortcodes for my posts (it is not elegant, but it works for me for now. I am new to Hugo, so I would appreciate a more elegant solution too from any of the experienced devs):
In your post’s frontmatter, you need to add the variables you want. e.g see the keyboard example below.
---
title: A
description: lorem ipsum
slug: /a-key-board/
keyboard:
name: Keyboard Name
price: keyboard price
---
Then create a folder inside ./layouts/shortcodes e.g. keyboard with the files name.html and price.html with the code shown below respectively.
{{ with $.Page.Params.keyboard }}
{{ .Name }}
{{ end }}
{{ with $.Page.Params.keyboard }}
{{ .Price }}
{{ end }}
Then in your content, call either shortcode where you want it to appear as either {{< keyboard/name >}} or {{< keyboard/price >}}.
Yes, that’s almost what I need. But in my case there are dozens of keyboard models (about 40 in total). So I should to make 80+ shortcuts? It’s very dont convenient. If there a keyboard information will be updated, each time I should to update 2 files? It’s easy to forget.
And also there maybe other keyboard characteristics (keys type, wireless/non wireless, for gaming or for work, keyboard type, etc). So for every single keyboard I should to create 5-10 shortcuts? It’s not right way I guess.
Also strange, that there imposible to build different shortcuts within single shortcode file. Then access a necessary shortcode using shortcode name or other way…
Your problem is that you are not reading and understanding the solutions provided to you. Please read again what everyone (including me) wrote on how shortcodes are used. I have been there before, frustrated with having to figure out how Hugo works, but it helps to read and reread again!
I did not write anywhere that it is impossible. I wrote what I use and it works for me. That’s why I put a comment for a more elegant solution by the experienced devs.
Just to clarify: You can exactely do that with these .inline shotcode blocks. It’s a container where you can place code inside that normally wouldn’t be rendered in content files. This is an example from the Bootstrap docs. https://raw.githubusercontent.com/twbs/bootstrap/v5.1.3/site/content/docs/versions.md
See {{< list-versions.inline >}}.