【问题标题】:Replace Dictionary values In Scriban template Engine在 Scriban 模板引擎中替换字典值
【发布时间】:2020-10-29 20:20:13
【问题描述】:

我正在尝试使用 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。 价值观。我知道我在渲染线上犯了一些错误。谁能指出我的错误或为此提供任何解决方案。 谢谢

【问题讨论】:

    标签: c# .net dictionary template-engine scriban


    【解决方案1】:

    代码中有一些错误。我不确定您是否还试图拥有文本描述的子服务列表。如果是这样,请参阅此链接,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-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 2014-07-06
      • 2011-08-05
      • 2021-08-31
      • 1970-01-01
      相关资源
      最近更新 更多