Backbone.JS View Template()

The Backbone.JS View Template() method creates reusable copies of markup. It also provides access to the instance data, while rendering the view.

Syntax:

View.Template (data)   

Parameters: data: This parameter is used to specify the data to be accessed while rendering.

Example:

<!DOCTYPE html>  
<html>
<head>  
<title>Example</title>  
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" 
type="text/javascript"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" 
type="text/javascript"></script>  
</head>  
<body>  
<div id="1"></div>  
<script type="text/javascript">  
var X = Backbone.View.extend({  
el: $('#1'),  
template: _.template("HELLO <%= msg %>"),  
initialize: function(){  
this.render();  
},  
render: function(){  
this.$el.html(this.template({msg: ' WORLD!!'}));  
}  
});  
var Y = new X();  
</script>  
</body>  
</html>

Output:

HELLO WORLD!!

Explanation: In the above example the Template() method is used to get access to the instance data.

Please follow and like us:
Content Protection by DMCA.com