2

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#で同じことを達成する方法は?

4

2 に答える 2

1

これは古い質問であることは知っていますが、あなたが求めていることを行う方法があることがわかりました。Nustache では、< 記号を使用してテンプレートまたは部分的なインラインを作成できるため、{{>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}}
于 2014-11-20T03:46:41.993 に答える