谐音模板下划线(就像在车把上)?(Partials template in underscore (

2019-06-25 23:25发布

我有一个骨干模型这样

var PeopleModel = Backbone.Model.extend({
defaults: {              
    "people": [
          { "username": "alan", "firstName": "Alan", "lastName": "Johnson", "phone": "1111", "email": "alan@test.com" },
          { "username": "allison", firstName: "Allison", "lastName": "House", "phone": "2222", "email": "allison@test.com" },
          { "username": "ryan", "firstName": "Ryan", "lastName": "Carson", "phone": "3333", "email": "ryan@test.com" },
          { "username": "ed", "firstName": "Edward", "lastName": "Feild", "phone": "4444", "email": "ed@test.com" },
          { "username": "phil", "firstName": "Philip", "lastName": "Doom", "phone": "5555", "email": "phil@test.com" },
          { "username": "gerald", "firstName": "Gerald", "lastName": "Butler", "phone": "6666", "email": "gerald@test.com" }
    ],
    "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模板引擎部分

Answer 1:

不,是下划线的模板没有本地部分支持。 但是,你可以把几乎你想里面的任何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/

这是不是很光滑,漂亮{{> ... }}在车把不得不强调的模板周围的JavaScript本身就是一个非常薄的包装,并且有些限制你。 您可以使用命名空间避免把事情直接在window或者您可以使用{variable: ...}选项_.template和包装,以建立您的标准助手。



Answer 2:

或者避免使用全球范围内,你可以在全局模板助手混合就像这样:

(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上。



Answer 3:

我认为这是类似于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'


文章来源: Partials template in underscore (just like in handlebars)?