'Generate .md files from database fields
for a school project I am making a webshop with the Hugo static website generator. And I have the products of the shop in .md files. I also have a database with a table called product with name, price, description etc with an admin page so the admin can add new products to the database. The problem now is that I somehow need to generate a .md file for the new product using the database information. My idea was to use an already existing .md file for a product and via search and replace, insert the information for the new product. But I have no clue how to realize that. I would appreciate any help. Thanks! Heres an example for an already exisiting product .md file
---
title: "Baklava"
date: 2019-10-17T11:22:16+06:00
draft: false
# meta description
description : ""
# product Price
price: "22.00"
beforeprice: ""
# Product Short Description
shortDescription: ""
#product ID
productID: "4"
#Gramm
name: "Wie viel Gramm?"
amount: "400g|700g[+6.00]|1000g[+10.00]"
# type must be "products"
type: "products"
# product Images
# first image will be shown in the product page
images:
- image: "images/products/Produkt 5/Unbenannt.png"
---
Eine Mischung an Baklava von bester Qualität. Sie haben die Möglichkeit die verschiedenen Spezialitäten vorort zu probieren und sich selbst zu überzeugen. Lassen Sie sich von einer Mischung an feinsten Baklavasorten überraschen.
Solution 1:[1]
I did face the same problem a few months ago. I create a template of MD file, like below. Then, I run a script (I choose Python, but you can use any language). It reads the database, open the template, replace all the %%PARAM%% by the corresponding values, and store the MD file where you want it.
---
title: "%%TITLE%%"
date: 2019-10-17T11:22:16+06:00
draft: false
# meta description
description : "%%DESCRIPTION%%"
# product Price
price: "%%PRICE%%"
beforeprice: ""
# Product Short Description
shortDescription: "%%SHORT_DESCRIPTION%%"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeremie |
