2012-07-02 8 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" 
} 

}); 

、以下のような基幹モデルを持っているがこれは私がhandlebars.jsを使用して部分的に実装する方法である私のテンプレート

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

です。

私の質問

1.Do私たちは似たものを持って、私は包みunderscore.jsテンプレートエンジンのパーシャルはどういう意味ですか?

2.Ifはので、どのように我々はunderscore.jsテンプレートエンジンで部分的実装ん

答えて

16

をなし、下線のテンプレートにはネイティブの部分的なサポートはありません。しかし、内部に入れたいJavaScriptをほとんど入れることができます。<% ... %>;特にあなた自身の関数を呼び出すことができますので、あまり難しくない部分的なものを追加することができます。あなたは、このようなテンプレートを持つことができ:

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

をしてからwindowpartial機能を追加します。

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

デモ:http://jsfiddle.net/ambiguous/HDuj5/9/

ハンドルバーに{{> ... }}ほどおしゃれできれいではありませんが、アンダースコアのテンプレートは、JavaScript自体の周りにある非常に薄いラッパーです。名前空間を使用して直接windowに置かないようにするか、{variable: ...} option to _.templateとラッパーを使用して標準ヘルパーを設定することができます。

+0

おかげで、あなたのフィドルはたくさん助け。私はこのケースでは "ウィンドウ"を使うことを完全に忘れていました。 Thnks again – bhargav

+1

第2引数としてデータを持つ '_.template()'の2つの引数バージョンがバージョン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 
     ); 
    } 
}); 

を呼び出すgithubの上underscore mixinへのリンクです。

+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'