helpers字符串中的Handlebars模板标记

Handlebars template tags in helpers string

本文关键字:Handlebars 字符串 helpers      更新时间:2023-09-26

是否可以将handleba模板标记放入生成的html字符串中?

例如:

Ember.Handlebars.helper(...) {
  return new Handlebars.SafeString("<button {{action go}}></button>");
}

我知道这行不通,但有人能提出正确的方法吗?(如果存在)
Html的来源是:

<button {{action go}}></button>    

但应该是这样的:

<button data-ember-action="x"></button>    

其中x是某个整数

干杯。

您可以在运行时编译并调用模板:

Ember.Handlebars.helper('trigger-action', function(options) {
  var compiledTemplate = Ember.Handlebars.compile('</h1><a href="#" {{action go}}>Click</a>');  
  return compiledTemplate(null, options);
});

样品http://emberjs.jsbin.com/uhedazok/1/edit

请记住,模板总是经过编译的,因此可能会出现一些性能问题。