0

I'd like to know if there is any solution to throw an error when a variable is not defined inside my template?

Example:

function hello() {
    var data = { foo: 'hello' };

    //data.bar is undefined, data.bar will be replaced by an empty string by underscore
    var render = _.template('<p><%= data.foo %><%= data.bar %></p>', {data: data}); 

    //Rendering okay? true|false
    //if (render...)

}

I'd like to throw an error if the variable data.boo is not defined after the call of the _.template function. Do you have any idea if it's possible to do something like that? I checked the documentation and didn't find anything interesting unfortunately...

Thanks

1
  • 2
    What you're trying to do just seems wrong and I think your code could be refactored not to need this behaviour. Can you summarise how do you want to handle the situation when data.foo is undefined? Commented Dec 2, 2013 at 20:20

3 Answers 3

2

For better or worse, Underscore will do this for you for top-level properties:

var data = { foo: 'hello' };
var rendered  = _.template('<p><%= foo %><%= bar %></p>', data);
// throws ReferenceError: bar is not defined

This is because Underscore templates use with, so top-level properties are treated like variables. If you want an error on a sub-object property, you'll need to add it in yourself, maybe using with as well:

var data = { foo: 'hello' };
// throw
var rendered  = _.template('<% with (data) { %><p><%= foo %><%= bar %></p><% } %>',
    { data: data });

Or you could use an explicit throw, e.g.

<% if (data.bar === undefined) throw "No bar!"; %>

at the beginning of your template. But really, why would you do it this way? If you know what you're checking for, far better to do it with an explicit pre-render check and deal with it there, and avoid using errors for control flow:

var rendered;
if (data.foo !== undefined) rendered = _.template("...", { data: data });
else {
    // do something else
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is a better answer than my own.
My template is actually an HTML with several dozen of variables, that's why I wanted to avoid to test all variables separately.
0

Like @mechanicalfish said, what you're attempting to do seems incorrect. You should be validating the data outside of the template. In either case, here is what you asked for:

function hello() {
    var data = { foo: 'hello' };

    //data.bar is undefined, data.bar will be replaced by an empty string by underscore
    var render = _.template('<p><%= data.foo %><% ' +
      '  if (!data.bar) { ' +
      '    throw "data.bar is undefined" ' +
      '  } else { ' +
      '    print(data.bar) ' +
      '  } ' +
      ' %></p>', {data: data}); 

    //Rendering okay? true|false
    //if (render...)

}

Comments

-1

JavaScript has try/catch support.

function hello() {
    var data = { foo: 'hello' };

   try {

    //data.bar is undefined, data.bar will be replaced by an empty string by underscore
    var render = _.template('<p><%= data.foo %><%= data.bar %></p>', {data: data}); 
    } catch (e){
          throw "data.bar is undefined";
    }


}

1 Comment

The render works perfectly even though the variable is undefined, it's my problem, there is no possibility to use try/catch there.

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.