javascript - Templating Error in Underscore js -


hi trying explore templating in underscore.js

so far created template string:

  var student = "<% _.each(students, function(elem, index, list){ %>" +     "<h1>" + "<%= elem.lastname %>" + " " + "<%= elem.firstname %>" + "</h1>" +     "<br/> <h2>" + "<%= elem.school %>" + "</h2>" +   "<% }); %>"; 

then on code called this:

  function appendtemplatedata(datastring) {         let container = document.getelementbyid("container");         container.innerhtml = container.innerhtml + datastring;     }   var result = _.template(student, students);   appendtemplatedata(result); 

on part:

var result = _.template(student, students); 

my template variable student while have external js file w/c array of objects ff info:

var students = [     {         "firstname": "woody",         "lastname" : "johnson",         "school" : "bloc",         "grade" : 12,         "midterm_score": 75,         "final_score": 85     },     {         "firstname" : "jerry",         "lastname" : "jones",         "school" : "hack reactor",         "grade" : 10,         "midterm_score": 50,         "final_score": 65     } ]; 

upon running code returns me error:

function (n){return o.call(this,n,m)} 

i not sure means. help? why there error that? how can fix it?

the correct call should be: _.template(student)(students), more compile template call it...the function compiled one, not error

 var student = "<% _.each(students, function(elem, index, list){ %>" +      "<h1>" + "<%= elem.lastname %>" + " " + "<%= elem.firstname %>" + "</h1>" +      "<br/> <h2>" + "<%= elem.school %>" + "</h2>" +    "<% }); %>";        var students = [      {          "firstname": "woody",          "lastname" : "johnson",          "school" : "bloc",          "grade" : 12,          "midterm_score": 75,          "final_score": 85      },      {          "firstname" : "jerry",          "lastname" : "jones",          "school" : "hack reactor",          "grade" : 10,          "midterm_score": 50,          "final_score": 65      }  ];      function appendtemplatedata(datastring) {          let container = document.getelementbyid("container");          container.innerhtml = container.innerhtml + datastring;      }     var compiledtemplated = _.template(student);   var result = compiledtemplated({students: students});    appendtemplatedata(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>      <div id="container">  </div>


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -