Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#应用程序中使用带nustach的Partials?_C#_Mustache - Fatal编程技术网

如何在c#应用程序中使用带nustach的Partials?

如何在c#应用程序中使用带nustach的Partials?,c#,mustache,C#,Mustache,我有一个使用Nustach的windows应用程序。我可以使用Nustache迭代对象或数组,但是如何使用Nustache进行部分枚举呢 检查样本11 var data = { depts: [ { name: "Engineering", employees: [ {firstName: "Christophe", lastName: "Coenraets"}, {firstName: "John", lastName: "Smith"}] },

我有一个使用Nustach的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);
var数据={depts:[
{名称:“工程”,
雇员:[
{姓:“克里斯托弗”,姓:“科恩雷茨”},
{姓:“约翰”,姓:“史密斯”}]
},
{名称:“销售”,
雇员:[
{姓:“宝拉”,姓:“泰勒”},
{姓:“丽莎”,姓:“琼斯”}]
}]     };
var tpl=“{{{depts}}{{name}}”+
“
    {{{{}雇员}{{{>雇员}{{/employees}
{{/depts}”; var partials={employee:“
  • {{firstName}}{{lastName}
  • ”}; var html=Mustache.to_html(tpl、数据、部分); $('#sampleArea').html(html);

    如何在C#?

    中实现相同的功能修改tpl,如下所示

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

    然后传递对象数组。它会正常地回响

    我知道这是一个老问题,但我发现有一种方法可以满足你的要求。 Nustach允许使用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}}
    
    64{{foo}
    
    因此,您在Nustach中的新代码是:

        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);
    
    string tpl=“{{employee}}{{/employees}}{{/depts}}”;
    字符串html=Nustache.Core.Render.StringToString(tpl,数据);
    
    使用此技术允许递归模板呈现,下面将呈现部门和员工的继承权

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