Pass parameters to template without overriding data context

前端 未结 2 1285
囚心锁ツ
囚心锁ツ 2020-12-05 20:26

I want to pass a new parameter to a template with keeping it\'s original data context.

  • original data context : {message:\"hello\"} {{> myTemplate withIc
2条回答
  •  难免孤独
    2020-12-05 21:05

    You can always extend the current context in a helper:

    Template.parentTemplate.helpers({
      iconContext: function() {
        var result = _.clone(this);
        result.withIcon = true;
        return result;
      }
    });
    

    And use it like this:

    
    

    Alternatively, you could create a more generic helper like this:

    Template.registerHelper('extendContext', function(key, value) {
      var result = _.clone(this);
      result[key] = value;
      return result;
    });
    

    And then choose the key/value pairs from within the html of any template:

    
    

    Either solution is more desirable than hiding the original data in a separate field, as it keeps the child template generic.

提交回复
热议问题