Home > Web Front-end > JS Tutorial > How to Resolve 'Underscore Template Variable Not Defined' Errors in Backbone.js?

How to Resolve 'Underscore Template Variable Not Defined' Errors in Backbone.js?

Susan Sarandon
Release: 2024-11-17 14:24:01
Original
834 people have browsed it

How to Resolve

Underscore Template Variable Not Defined Error

In Backbone.js, using Underscore templates to populate data into HTML can result in an error if the template variables are not defined. This issue arises when attempting to render a template using the older syntax of Underscore 1.6 and below, which allowed direct parsing and filling of templates in one step.

var html = _.template('<%= lat %> <%= lon%>', data);
Copy after login

However, in Underscore 1.7 and above, the second argument to _.template serves as template options rather than the data itself. To correctly render the template, it must be compiled first, and then the compiled function can be executed with the data.

var tmpl = _.template('<%= lat %> <%= lon %>');
var html = tmpl(data);
Copy after login

To resolve the "variable not defined" error, update the Backbone.js application to use the correct template compilation syntax as demonstrated above. By following these updated template handling techniques, Backbone.js developers can ensure their templates are rendered properly and avoid template-related errors.

The above is the detailed content of How to Resolve 'Underscore Template Variable Not Defined' Errors in Backbone.js?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template