0

I tried with this question to override template function globally using require function and it works well for templateSetting.

I tried to extend/override the _.template function to wrap div using below code. But getting with plain template not parsed template like this <%= title %>

    define(['underscoreBase'], function(_) {
    _.template = function(str){
        return "<div class='test'>"+str+"</div>";
    };
    return _;
});

How do we acheive adding/wrap some HTML tag with _.template string content? Please advise.

Community
  • 1
  • 1
mymotherland
  • 7,968
  • 14
  • 65
  • 122

2 Answers2

0

You need to keep a reference to the original template function:

define(['underscoreBase'], function(_) {
    var oldTemplate = _.template;
    _.template = function(str){
        return oldTemplate("<div class='test'>"+str+"</div>");
    };
    return _;
});
Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66
0

I have implemented it like

var oldTemplate = _.template;
    function template(str, data) {
        return oldTemplate(
            "<div class='test'>"+str+"</div>",
            data
        );
    }
_.mixin({ template:template });
return _;
mymotherland
  • 7,968
  • 14
  • 65
  • 122