【问题标题】:Scriban Template Engine Multi loop SupportsScriban 模板引擎多循环支持
【发布时间】:2020-10-31 01:33:46
【问题描述】:

我正在尝试使用 Scriban 模板引擎来支持多循环。 例如

string bodyTextSub = "{{ for service in services }} ServiceName: {{ service }} {{ end }}" +
                "{{ for subservice in subServiceList }} SubServiceName: {{ subservice }} {{ end }}";
List<string> subServiceList = new List<string>
            {
                "PingSubService",
                "UrlSubService"
            };
            Dictionary<string, List<string>> serviceDictionary = new Dictionary<string, List<string>>()
            {
                {"emailContent", subServiceList},
                {"mailContent", subServiceList}
            };

            var template2 = Template.Parse(bodyTextSub);
            var result2 = template2.Render(new { services = serviceDictionary });
            Console.WriteLine(result2.ToString());

我得到像这样的输出

ServiceName: {key: emailContent, value: [PingSubService, UrlSubService]}

我希望基于我们应该在子服务中循环的键,但它没有发生。 任何人都可以帮助我吗?

我的第二个问题是 Scriban 模板引擎支持嵌套循环吗? 提前致谢

【问题讨论】:

  • 我还没有收到任何人的回复。没有人用过 Scriban 模板引擎?

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


【解决方案1】:

Scriban 确实支持嵌套循环。我已经更新了您的代码以显示您将如何执行此操作。我已根据您的要求更新了代码和结果。

var bodyTextSub = @"{{ for service in services }} 
ServiceName: {{ service.key }} {{$counter = 1}}
SubServices: {{ for subService in service.value }}{{ `
  `; $counter + `. `; $counter = $counter + 1 ; }}{{ subService }}{{ end }}
{{ end }}";
var subServiceList = new List<string>
{
    "PingSubService",
    "UrlSubService"
};
var serviceDictionary = new Dictionary<string, List<string>>()
{
    {"emailContent", subServiceList},
    {"mailContent", subServiceList}
};

var template2 = Template.Parse(bodyTextSub);
var result2 = template2.Render(new {services = serviceDictionary});
Console.WriteLine(result2.ToString());

这将导致以下输出:

ServiceName: emailContent 
SubServices: 
  1. PingSubService
  2. UrlSubService
 
ServiceName: mailContent 
SubServices: 
  1. PingSubService
  2. UrlSubService

【讨论】:

  • 非常感谢您的宝贵意见。
  • :感谢您的回复。我当时正在度假。关于上述答复,我还有一个问题。目前,对于我们用逗号分隔的一个或多个子服务,您能否举例说明或修改代码,以便我们将用数字代替逗号打印在新行中。例如 1.PingSubService 2.UrlSubService 等等。谢谢
猜你喜欢
  • 1970-01-01
  • 2020-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 2021-06-07
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多