Replace Dictionary values In Scriban template Engine

I am trying to use Scriban template engine for replacing the Dictionary Values. For Example

string bodyText = "Hi,            The following service(s) has reported issues.            {{emailContent}}         Thanks " ; Dictionary<string, string> keyValuePairs = new Dictionary<string, string>() {    {"emailContent",  "UserManagement has following unhealthy subservice(s)"} };     var template1 = Template.Parse(bodyText);   var result1 = template1.Render(new { emailContent = keyValuePairs[emailContent] });   Console.WriteLine(result1.ToString()); 

But I am getting an error at the Render line. Basically want to replace that emailContent with Dictionary. values. I know I am doing some mistakes at render line. Can anyone point out my mistake or give any solution for that. Thanks

Add Comment
1 Answer(s)

There are a few mistakes in the code. I’m not sure if you are trying to also have a list of sub-services as the text depicts. If so, see this link, https://stackoverflow.com/a/62902173/6326441.

var bodyText = @"Hi, The following service(s) has reported issues:{{ for service in services }}     ""{{ service.key }}"": ""{{service.value}}""{{end}} Thanks "; var keyValuePairs = new Dictionary<string, string>() {     {"UserManagement",  "UserManagement has following unhealthy subservice(s)"},     {"DNS",  "Network has following unhealthy subservice(s)"} };   var template1 = Template.Parse(bodyText); var result1 = template1.Render(new { services = keyValuePairs }); Console.WriteLine(result1.ToString()); 

This will result in the following:

Hi, The following service(s) has reported issues:     "UserManagement": "UserManagement has following unhealthy subservice(s)"     "DNS": "Network has following unhealthy subservice(s)" Thanks  
Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.