2013-03-21 1 views
2

나는 Nustache를 사용하는 Windows 응용 프로그램을 가지고 있습니다. Nustache를 사용하여 객체 또는 배열을 반복 할 수 있지만 Nustache를 사용하여 부분적 Enumeration을 수행하는 방법은 무엇입니까?C# 응용 프로그램에서 Nustache와 함께 Partials를 사용하는 방법?

Check this link

검사 샘플

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

11. 방법 C에서이를 달성하기 위해?

답변

1

아래 그림과 같이 tpl을 수정하십시오!

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

그런 다음 개체 배열을 전달하십시오. 제대로 가져올 것입니다. !!!

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}} 
관련 문제