2011-07-27 12 views
14

Jak mogę użyć szablonu zagnieżdżonego w wąsy? Czy istnieje sposób, aby zrobić to samo?Szablony do wąsowania: szablony zagnieżdżone

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

Mam nadzieję, że masz pytanie. Nie dodałem znaku escape dla ważności js, ponieważ kod jest dzielony na różne linie.

+1

Dlaczego nie użyć partials? https://mustache.github.io/mustache.5.html#Partials – Pere

Odpowiedz

7

Można użyć lambda zagnieździć szablon:

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

Pomyślałem o tej pracy aorund .. ale myślałem, że może być coś wbudowanego w wąsy .. Mogę się mylić ... – Harry

+0

@Harry: Nie, Jedynym sposobem są lambdy, ponieważ częściowe elementy nie mogą uzyskać parametrów (chyba że je zhakujesz: http://stackoverflow.com/questions/6656093/mustache-partials-using-variable-syntax- without-the). – marc

+0

Byłbym wdzięczny, gdybyś mógł skierować mnie do jakiejś strony, która daje dobrą znajomość wąsów. – Harry

7

zrobiłem przykład zagnieżdżonych szablonów na co jsFiddle. Tutaj jest to w szczegółach:

Najpierw skonfiguruj swoją HTML

<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>​ 

Następnie javascript (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

Poszedłem w tym samym kierunku, z wyjątkiem umieszczenia nazwy szablonu podrzędnego w szablonie, np. {{#nested}} tpl-nested {{/ nested}}, zobacz https://jsfiddle.net/omnius/env3d7wk/ ale problem, na który natrafiłem, polegał na tym, że nie działał podczas zagnieżdżania wielu poziomów i nie wiem, dlaczego to nie działa.Sądziłem, że zrozumiałem, że ponieważ używałem tego samego renderera w dół, powinien on używać tego samego modelu danych, który obejmuje moduł ładujący pod-szablon. Masz pomysł, dlaczego mój Fiddle nie wykonuje pod-pod-szablonów? –

-1

Oto metoda, w której robimy wymianę strun zanim kompilacji szablony. Subtemplates nazywane są w szablonach przez: {{#template}} insertTheNameOfYourSubTemplateHere {{/ szablon}}

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