1

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.

2
  • Assigning to _.templateSettings outside the function should work assuming of course that underscore.js is loaded before you mess with _.templateSettings. PS: You should set all three regexes in templateSettings if you want to change the delimiters. Commented Dec 14, 2012 at 2:56
  • let me revisit that; that's what I tried first but being in treacherous waters, thought I'd ask. I think ur right and I made a mistake earlier. Commented Dec 14, 2012 at 4:56

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Thx, SAR, I have posted un updated fiddle which shows yours but with the problem I am having.
jsfiddle.net/vMHeq/2 When you have underscore templates and want to have a carrage return you have to end the line with an escape character, \. Also you had a couple typos in the fiddle. Check this out and see if it answers your questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.