如何在c#应用程序中使用分形和胡须

本文关键字:分形 胡须 应用程序 | 更新日期: 2023-09-27 18:05:09

我有一个使用Nustache的windows应用程序。我可以迭代一个对象或数组使用Nustache,但如何做部分枚举使用Nustache?

查看此链接

检查样本11。

var data = { depts: [
{   name: "Engineering",
    employees: [
        {firstName: "Christophe", lastName: "Coenraets"},
        {firstName: "John", lastName: "Smith"}]
},
{   name: "Sales",
    employees: [
        {firstName: "Paula", lastName: "Taylor"},
        {firstName: "Lisa", lastName: "Jones"}]
}]     };
 var tpl = "{{#depts}}<h1>{{name}}</h1>" +
          "<ul>{{#employees}}{{>employee}}{{/employees}}</ul>{{/depts}}";
var partials = {employee:"<li>{{firstName}} {{lastName}}</li>"};
var html = Mustache.to_html(tpl, data, partials);
$('#sampleArea').html(html);

如何在c#中实现相同的功能?

如何在c#应用程序中使用分形和胡须

修改tpl如下图所示!

var tpl = "{{employee}}<li>{{firstName}} {{lastName}}</li>{{/employee}}{{#depts}}<h1>{{name}}</h1>" +
      "<ul>{{#employees}}{{>employee}}{{/employees}}</ul>{{/depts}}";

然后传递对象数组。它会正常取回的!!

我知道这是一个老问题,但我发现有一种方法可以做到你所要求的。Nustache允许使用<符号{{>employee}}你的部分模板这里{{/employee}}将是你的部分,然后当你想引用它时,只需使用>符号,例如:{{>employee}}

来自readme.txt文件

64      {{<foo}}This is the foo template.{{/foo}}
65      The above doesn't get rendered until it's included
66      like this:
67      {{>foo}}

所以你在nustache中的新代码将是:

    string tpl = "{{<employee}}<li>{{firstName}} {{lastName}}</li>{{/employee}}" +
"{{#depts}}<h1>{{name}}</h1><ul>{{#employees}}{{>employee}}{{/employees}}</ul>{{/depts}}";
    string html = Nustache.Core.Render.StringToString(tpl, data);

使用此技术允许递归模板呈现,下面将呈现部门和员工的层次结构

{{<employee}}<li>{{firstname}} {{lastname}}{{>dept}}</li>{{/employee}}
{{<dept}}   
    <ul>                            
        <li >
            <span >{{name}}</span>                  
            {{#employees}}                          
                {{>employee}}
            {{/children}}           
        </li>                       
    </ul>
{{/dept}}{{>dept}}