-1

The data that I'm trying to pass to my template is inexplicably ignored when it gets to underscore's template renderer.

The data is available:

失败前开发控制台中的 underscore.js

Yet I get an exception in the console that the param url is undefined:

Uncaught ReferenceError: url is not defined

If I drill a bit deeper, I get to some crazy-looking javascript:

UnderscoreJS 在模板上的渲染器

It fails immediately after: __p+='\n\t\t<a href="#'+ (notice url and label are still both defined).

4

1 回答 1

1

Normally there would be a with(obj||{}) in the compiled template. I don't see one in yours so perhaps the variable option is in use somewhere:

By default, template places the values from your data in the local scope via the with statement. However, you can specify a single variable name with the variable setting.

For example, if you:

var t = _.template('<%= x %>');

and then look at t.source, you'll see a function like this (formatted for clarity):

function(obj){
    //...
    with(obj||{}){
        //...
    }
    return __p;
}

but if you say:

var t = _.template('<%= x %>', null, { variable: 'E' });

and look at t.source, you'll see the same structure without the with block around the guts of the function.

Demo: http://jsfiddle.net/ambiguous/jrEub/

The structure of the template function suggests that you have {variable: 'E'} somewhere so your template should be look at <% E.url %> and such instead of just <% url %>.

于 2013-12-07T19:21:14.843 回答