2012-07-02 37 views
10

我有這樣下劃線中的部分模板(就像在把手中)?

var PeopleModel = Backbone.Model.extend({ 
defaults: {    
    "people": [ 
      { "username": "alan", "firstName": "Alan", "lastName": "Johnson", "phone": "1111", "email": "[email protected]" }, 
      { "username": "allison", firstName: "Allison", "lastName": "House", "phone": "2222", "email": "[email protected]" }, 
      { "username": "ryan", "firstName": "Ryan", "lastName": "Carson", "phone": "3333", "email": "[email protected]" }, 
      { "username": "ed", "firstName": "Edward", "lastName": "Feild", "phone": "4444", "email": "[email protected]" }, 
      { "username": "phil", "firstName": "Philip", "lastName": "Doom", "phone": "5555", "email": "[email protected]" }, 
      { "username": "gerald", "firstName": "Gerald", "lastName": "Butler", "phone": "6666", "email": "[email protected]" } 
    ], 
    "company": {"name": "Random Corp."}, 
    "country": "England" 
} 

}); 

且低於骨幹模型是我的模板

<script id="people-template" type="text/x-handlebars-template"> 
{{#each people}} 
    {{> person}} 
{{/each}} 
</script> 

<script id="person-partial" type="text/x-handlebars-template"> 
<div class="person"> 
    <h2>{{fullName}} </h2> 
    <div class="phone">{{phone}}</div> 
    <div class="email"><a href="mailto:{{email}}">{{email}}</a></div>  
</div> 

我這是怎麼實現的使用handlebars.js部分。

我的問題

1.Do我們有類似的事情,我的意思是櫃面underscore.js模板引擎的諧音?

2,如果這樣,我們如何實現underscore.js模板引擎

回答

16

沒有,是下劃線的模板沒有本地部分支持部分。但是,您可以在<% ... %>之內放入幾乎所有您想要的JavaScript;特別是,你可以調用你自己的函數,這樣你就可以毫無困難地添加一些部分。你可以有這樣的一個模板:

<script id="people-template" type="text/x-handlebars-template"> 
    <% _(people).each(function(person) { %> 
     <%= partial('person', person) %> 
    <% }) %> 
</script> 

,然後添加一個partial功能window

window.partial = function(which, data) { 
    var tmpl = $('#' + which + '-partial').html(); 
    return _.template(tmpl)(data); 
}; 

演示:http://jsfiddle.net/ambiguous/HDuj5/9/

這是不是很光滑,漂亮,{{> ... }}在車把但Underscore的模板是JavaScript本身的一個非常簡單的包裝,它限制了你的工作。您可以使用命名空間來避免將東西直接放在window中,或者您可以使用{variable: ...} option to _.template和包裝來設置標準助手。

+0

謝謝你耐心地回答我的問題,你的小提琴幫了不少忙。我完全忘記了爲這種情況使用「窗口」。再次請注意 – bhargav

+1

請注意,數據作爲第二個參數的'_.template()'的雙參數版本已從版本1.7開始刪除。儘管如此,這種方法仍然很有用。 –

+0

@PeterV.Mørch:謝謝你的提醒。我實際上回答了一些「爲什麼'_.template(tmpl,data)'工作?」最近的問題。 –

13

或避免使用全球範圍內,你可以在全局模板助手混合像這樣:

(function() { 
    var originalUnderscoreTemplateFunction = _.template; 
    var templateHelpers = {}; 

    _.mixin({ 
     addTemplateHelpers : function(newHelpers) { 
      _.extend(templateHelpers, newHelpers); 
     }, 

     template : function(text, data, settings) { 
      // replace the built in _.template function with one that supports the addTemplateHelpers 
      // function above. Basically the combo of the addTemplateHelpers function and this new 
      // template function allows us to mix in global "helpers" to the data objects passed 
      // to all our templates when they render. This replacement template function just wraps 
      // the original _.template function, so it sould be pretty break-resistent moving forward. 

      if(data) 
      { 
       // if data is supplied, the original _.template function just returns the raw value of the 
       // render function (the final rentered html/text). So in this case we just extend 
       // the data param with our templateHelpers and return raw value as well. 

       _.defaults(data, templateHelpers); // extend data with our helper functions 
       return originalUnderscoreTemplateFunction.apply(this, arguments); // pass the buck to the original _.template function 
      } 

      var template = originalUnderscoreTemplateFunction.apply(this, arguments); 

      var wrappedTemplate = function(data) { 
       _.defaults(data, templateHelpers); 
       return template.apply(this, arguments); 
      }; 

      return wrappedTemplate; 
     } 
    } 
} 

然後調用

_.addTemplateHelpers({ 
    partial : function() { 
     return _.template(
      $('#' + which + '-partial').html(), 
      data 
     ); 
    } 
}); 

這裏是給underscore mixin在GitHub上的鏈接。

+0

你能用_.partial和_.template做些什麼來達到這個目的嗎? – backdesk

+0

_.partial與模板偏好無關:它在函數中填充參數。 – Dtipson

1

我認爲這是類似於Dave的答案,但也許需要更少的代碼:

function partialTemplate(origTemplate, partialValues){ 
    return function(values){ 
     return origTemplate(_.defaults(values, partialValues)); 
    }; 
} 

用法示例:

var t = _.template('<%= val1 %>,<%= val2 %>'); // original template requiring 2 values 
var pt = partialTemplate(t, {val1:1}); // partial template with 1 value pre-populated 
pt({val2:2}); // returns '1,2' 
pt({val2:3}); // returns '1,3'