0

我正在尝试使用 Scriban 模板引擎来替换字典值。例如

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());

但我在渲染行出现错误。基本上想用字典替换那个emailContent。价值观。我知道我在渲染线上犯了一些错误。谁能指出我的错误或为此提供任何解决方案。谢谢

4

1 回答 1

0

代码中有一些错误。我不确定您是否还试图拥有文本描述的子服务列表。如果是这样,请参阅此链接,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());

这将导致以下结果:

Hi,
The following service(s) has reported issues:
    "UserManagement": "UserManagement has following unhealthy subservice(s)"
    "DNS": "Network has following unhealthy subservice(s)"
Thanks 
于 2020-07-14T19:11:21.350 回答