Scriban Template Engine Multi loop Supports

こ雲淡風輕ζ 提交于 2020-07-23 06:20:54

问题


I am trying to use Scriban Template Engine for multiple loop support. For Example

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

I am getting the output like

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

I want that based on the key we should loop in the subservices but it is not happening. Can anyone help me in this ?

My second question does Scriban Template Engine Supports nested looping ? Thanks in Advance


回答1:


Scriban does support nested looping. I've updated your code to show how you would do this.

var bodyTextSub = @"{{ for service in services }} 
ServiceName: {{ service.key }} {{$counter = 0}}
SubServices: {{ for subService in service.value }}{{ if $counter > 0; `,` ; end ; $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());

This will result in the following output:

ServiceName: emailContent 
SubServices: PingSubService, UrlSubService
 
ServiceName: mailContent 
SubServices: PingSubService, UrlSubService


来源:https://stackoverflow.com/questions/62832082/scriban-template-engine-multi-loop-supports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!