Tell me, please, how to create a template in Backbone.js, if there is any embedded data.

Sample data:

var questions = [ { id: 0, question: [ { id: 101, text: 'Ваша фамилия?', }, { id: 102, text: 'Ваше имя?', }, { id: 103, text: 'Ваше отчество?', }, { id: 104, text: 'Ваш возраст?', }, ] } 

];

And html-code for output:

 <div id="qu_0" class="questions"><div> <p><input type="radio" rel="question" name="question_0" id="i101" value="101" /><label for="i101">Ваша фамилия?</label></p> <p><input type="radio" rel="question" name="question_0" id="i102" value="102" /><label for="i102">Ваше имя?</label></p> <p><input type="radio" rel="question" name="question_0" id="i103" value="103" /><label for="i103">Ваше отчество?</label></p> <p><input type="radio" rel="question" name="question_0" id="i104" value="104" /><label for="i104">Ваш возраст?</label></p> <button type="button" name="next"><b>Ответить</b></button> </div></div> 

I understand that it should turn out somehow like this:

 <script id="questionTemplate" type="text/template"> <div> <p><input type="radio" rel="question" name="question_<%= id %>" id="i<%= q.id %>" value="<%= q.id %>" /><label for="i<%= q.id %>"><%= q.text %></label></p> <button type="button" name="next"><b>Ответить</b></button> </div> 

</ script>

But did not figure out how to display the nested array of questions in the template. Tell me please.

    1 answer 1

    Understood - it is done using _.each ();

     <script id="questionTemplate" type="text/template"> <div> <% _.each(question, function(q) { %> <p><input type="radio" rel="question" name="question_<%= id %>" id="i<%= q.id %>" value="<%= q.id %>" /><label for="i<%= q.id %>"><%= q.text %></label></p> <% }); %> <button type="button" name="next"><b>Ответить</b></button> </div> </script>