I am trying to create generic utility class that builds email template body taking in two inputs
- html template with placeholders (it can have repeating records)
- json data
Expected output is to generate html file combining html template and jsondata. As I do not know all the templates or json data schema in advance, I may have to use Dynamic/Expando object or classes related to Json parsing
Here's the example Sample html
<ul id='products'>
{{ for product in products }}
<li>
<h2>{{ product.name }}</h2>
Price: {{ product.price }}
{{ product.description }}
</li>
{{ end }}
</ul>
Sample Data
{
"products": [
{
"name": "Ball",
"price": 788.0,
"description": "Matches Ball"
},
{
"name": "Bat",
"price": 2000.0,
"description": "Wooden Bat"
}
]
}
I have tried using https://github.com/lunet-io/scriban and https://www.nuget.org/packages/Newtonsoft.Json/ but could not get around the issues. I am open to other suggestions
public string RenderHtml(string templateHTML, string jsonData)
{
Scriban.Template template = Scriban.Template.Parse(templateHTML);
JObject jsonObject = JObject.Parse(jsonData);
return template.Render(jsonObject);
}