2012-05-25 2 views

답변

0

동일한 HTML 파일에 정의 된 부분을 템플릿으로 사용하는 방법을 묻고 싶습니다. 이 예제에서는이를 보여주기 위해 두 개의 별도 템플릿에서 동일한 부분을 사용합니다. 다른 예를 찾고 계시다면 알려주십시오.

여기, 두 개의 서로 다른 템플릿과 여기에
<script type=text id=tpl1> 
    {{greeting}} {{> planet}} 
</script> 

<script type=text id=tpl2> 
    {{planet}} 
</script> 

<script type=text id=tpl3> 
    {{> planet}} {{goal}} 
</script>​ 

이러한 템플릿을 찾을 수있는 자바 스크립트입니다

부분 하나 자신의 데이터를 준비하고, 재사용 부분을 렌더링.

// get template html 
var tpl1 = $('#tpl1').html(), 
    tpl2 = $('#tpl2').html(), // partial 
    tpl3 = $('#tpl3').html(), 
// define data for each template 
    tpl1Data = { 'greeting' : 'hello'}, 
    tpl2Data = { 'planet' : 'world'}, 
    tpl3Data = { 'goal' : 'peace'}, 
// define partial object 
    partial = {'planet' : tpl2 }, 
// render to html combining template view and partial view and defining partial 
    html1 = Mustache.to_html(tpl1, jQuery.extend(tpl1Data, tpl2Data), partial), 
    html2 = Mustache.to_html(tpl3, jQuery.extend(tpl3Data, tpl2Data), partial); 
// write compiled Mustache templates to document 
document.write(html1 + ', let\'s try for ' + html2); 

see this example in its working entirety on jsFiddle 수 있습니다.

두 번째 템플릿에서 부분 데이터에 다른 데이터를 사용하려면 tpl2bData = { 'planet' : 'mars' }과 같이 정의하고 html2을 렌더링 할 때 참조 할 수 있습니다.

html2 = Mustache.to_html(tpl3, jQuery.extend(tpl3Data, tpl2bData), partial); 
관련 문제