Underscore.js _.template causes error from Chrome extension

后端 未结 6 653
北荒
北荒 2020-12-16 12:52

If I use underscore.js\'s _.template() from inside a Google Chrome extension I get the following error in the console:

Uncaught Error: Code generation

6条回答
  •  天命终不由人
    2020-12-16 13:51

    You can write your own template mini-engine using jQuery's $('') construction.

    The clean way:

    function myElement(text) {
      var d = $('
    '); d.text(text); return d; } myElement('my text').appendTo(domParent);

    The dirty way:

    var yourTemplate = '
    ${somevar}
    '; function instTemplate(tmpl, text) { return $(tmpl.replace(/\$\{somevar\}/g, text)); } instTemplate(yourTemplate, 'your text').appendTo(domParent);

    E.g. it's pretty quick to rewrite simple jquery.tmpl templates using the dirty method, if you know that the replacement data is not harmful, etc.

提交回复
热议问题