2011-07-27 56 views
14

Làm cách nào để sử dụng mẫu lồng nhau trong bộ ria mép? Có cách nào để làm tương tự không?Moustache Templating: các mẫu lồng nhau

var tmpl="{{#data}} 
{{values}} 
Name: {{name}} 
//{{another_templ({{name.value}})}} 
{{/values}} 
{{/data}}" 

Mong các bạn có câu hỏi. Tôi đã không thêm ký tự thoát cho giá trị js vì mã được chia thành các dòng khác nhau.

+1

Tại sao bạn không sử dụng partials? https://mustache.github.io/mustache.5.html#Partials – Pere

Trả lời

7

Bạn có thể sử dụng một lambda để tổ mẫu:

function nested_template(template_string, translate) { 
    return function() { 
    return function(text, render) { 
     return Mustache.to_html(template_string, translate(render(text))); 
    }; 
    }; 
} 

var template_string = 
    "{{#data}}"+ 
    "{{values}}"+ 
     "Name: {{name}}"+ 
     "{{#another_templ}}{{name}}{{/another_templ}}"+ 
    "{{/values}}"+ 
    "{{/data}}"; 

var another_template_string = "<b>{{name}}</b>"; // for example 

var view = { 
    data: { 
    values: { 
     name: "Test" 
    } 
    }, 
    another_templ: nested_template(another_template_string, function(text) { 
    return {name: text}; 
    }); 
}; 

var result = Mustache.to_html(template_string, view); 
+0

Tôi nghĩ về công việc này aorund .. nhưng tôi nghĩ rằng có thể có một cái gì đó được xây dựng vào ria mép .. Tôi có thể sai .. – Harry

+0

@Harry: Không, cách duy nhất là lambdas, bởi vì partials không thể nhận được các tham số (ngoại trừ bạn hack chúng: http://stackoverflow.com/questions/6656093/mustache-partials-using-variable-syntax-without-the). – marc

+0

Tôi sẽ đánh giá cao nếu bạn có thể hướng dẫn tôi đến một số trang web cung cấp cho một mustache abt mustache tốt .. – Harry

7

Tôi đã thực hiện một ví dụ về các mẫu lồng nhau qua tại jsFiddle. Dưới đây là chi tiết:

Thứ nhất, thiết lập HTML của bạn

<div class="main"><!-- content here --></div> 

<script type="text/html" id="tpl"> 
    {{#data}} 
     {{#names}} 
      Name: {{name}} 
      {{#nested}}{{name}}{{/nested}}<br> 
     {{/names}} 
    {{/data}} 
</script> 

<script type="text/html" id="tpl-nested"> 
    &mdash; <b>{{name}}</b> 
</script>​ 

Sau đó, javascript (sử dụng jQuery)

function renderNested(template_string, translate) { 
    return function() { 
     return function(text, render) { 
      return Mustache.to_html(template_string, translate(render(text))); 
     }; 
    }; 
} 

var template = $("#tpl").html(); 

var nested_template = $("#tpl-nested").html(); 

var model = { 
    data: { 
     names: [ 
      { name: "Foo" }, 
      { name: "Bar" } 
     ], 
     nested: renderNested(nested_template, function(text) { 
      return { name: text }; 
     }) 
    } 
}; 

var result = Mustache.to_html(template, model); 

$(".main").html(result); 
+0

Tôi đã đi một hướng tương tự, ngoại trừ tôi đặt tên của mẫu con trong mẫu, như {{#nested}} tpl-lồng nhau {{/ lồng nhau}}, xem https://jsfiddle.net/omnius/env3d7wk/ nhưng vấn đề tôi gặp phải là nó không hoạt động khi lồng ghép nhiều cấp độ và tôi không biết tại sao nó không hoạt động.Tôi nghĩ rằng tôi đã hiểu rằng vì tôi đã sử dụng cùng một trình kết xuất đồ họa, nên sử dụng cùng một mô hình dữ liệu, bao gồm trình tải mẫu phụ. Bất kỳ ý tưởng tại sao Fiddle của tôi không làm sub-sub-templates? –

-1

Đây là một phương pháp mà chúng tôi làm thay thế chuỗi trước khi chúng tôi biên dịch các mẫu. Subtemplates được gọi là trong mẫu theo: {{}} #template insertTheNameOfYourSubTemplateHere {{/ template}}

templates = {} 

function compileTemplates(templateNamesArray) { 
    for (index in templateNamesArray) { 
     var templateName = templateNamesArray[index]; 
     var baseHTML = $('#' + templateName).html(); 

     var start = baseHTML.indexOf("{{#template}}"); 
     while(start != -1) { 
      var end = baseHTML.indexOf('{{/template}}', start); 
      var nestedTemplateName = baseHTML.slice(start + "{{#template}}".length, end); 
      var nestedTemplateEl = $('#' + nestedTemplateName); 
      if (nestedTemplateEl.length == 0) { 
       throw "Could not find nested template '" + nestedTemplateName + "' for the template '" + templateName + "'"; 
      } 
      baseHTML = baseHTML.slice(0, start) + nestedTemplateEl.html() + baseHTML.slice(end + '{{/template}}'.length); 
      start = baseHTML.indexOf("{{#template}}", start); 
     } 
     templates[templateName] = Handlebars.compile(baseHTML); 
    } 
} 

compileTemplates(["templateActiveThreadTab", "templateActiveThreadContent", "templateTodoItem"]); 
Các vấn đề liên quan