the following code not generating html.
underscore template:
<script type="text/template" id="features-template"> <li class="list-group-item"> <span class="badge"><%= score %></span> <%= prediction %> </li> </script> json:
{ "features": { "status": true, "response": { "predictions": [ ["shirt", "90.12"], ["jeans", "09.88"] ] } } } jquery code:
var predictions = []; _.each(response.features.response.predictions, function(prediction, i) { predictions.push({ prediction: prediction[0], score: prediction[1] }); }); var tpl = _.template(featuretemplate, predictions)); console.log( tpl); i can see predictions array created values.
why doesn't code generate proper html?
i tested latest version of underscore (1.8.3), other versions may have differences.
the documentation of _.template says data given after compilation of template:
var compiled = _.template(featuretemplate); var tpl = compiled({predictions:predictions}); i added foreach loop in template:
<script type="text/template" id="features-template"> <% _.each(predictions, function(prediction) { %> <li class="list-group-item"> <span class="badge"><%= prediction.score %></span> <%= prediction.prediction %> </li> <% }); %> </script>
Comments
Post a Comment