Scriban 模板引擎多循环支持
Scriban Template Engine Multi loop Supports
我正在尝试使用 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 支持嵌套循环。我已经更新了您的代码以显示您将如何执行此操作。我已经根据您的要求更新了代码和结果。
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
我正在尝试使用 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 支持嵌套循环。我已经更新了您的代码以显示您将如何执行此操作。我已经根据您的要求更新了代码和结果。
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