timpone
timpone

Reputation: 19969

javascript scope with underscore and object literal

I have the following code and want to set all my underscore templates to use mustache syntax. If I move the _.templateSetting outside the function, it doesn't work. How would I set this globally?

thx in advance

arc_eh.mb={
  show_tree:function(){
    // all i want to do is move this outside and set globally 
    //  for all my underscore microtemplates
    _.templateSettings = {
      interpolate : /\{\{(.+?)\}\}/g
    };

    var template=_.template("hello {{ some }}");
    var jt = { "some" : "more-something" };
    //var final=template({ some : "say-something" });
    var final=template(jt);
    console.log(final);

update #1

So I've updated your fiddle with the problem I am having here: http://jsfiddle.net/vMHeq/1/

I AGREE that you fiddle works fine - I know this is a scope issue that I'm not familiar with. Any help appreciated.

Upvotes: 1

Views: 323

Answers (1)

AlbertEngelB
AlbertEngelB

Reputation: 16436

Basically, you're gonna need to set _.templateSettings options outside of your arc_eh.mb object. If you want to use all the power of _.template you'll want to include all three settings. I'm using {{- }}, {{= }}, and {{ }} for the formatting on this.

_.templateSettings = {
    interpolate: /\{\{\=(.+?)\}\}/gim,
    escape: /\{\{\-(.+?)\}\}/gim,
    evaluate: /\{\{([\s\S]+?)\}\}/gim
};

Set up a simple fiddle to show you what's up.

http://jsfiddle.net/AbLA8/1/

Upvotes: 1

Related Questions