Scriban模板引擎多循环支持 [英] Scriban Template Engine Multi loop Supports

查看:452
本文介绍了Scriban模板引擎多循环支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Scriban模板引擎提供多循环支持. 例如

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

我得到的输出就像

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 ?

我的第二个问题是Scriban模板引擎是否支持嵌套循环? 在此先感谢

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

推荐答案

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

Scriban does support nested looping. I've updated your code to show how you would do this. I've updated the code and the results per your request.

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模板引擎多循环支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆