Monday 15 August 2011

json - Backbone underscore template, how to render each child elem -


Hi, I am currently learning the spinal cord and got stuck.

Actually I'm reading the content under the JSON file and rendering to the Underscore template. I am able to send the parameters to my template and present them with the following code:

  var template = _.template (movieview), MovInfo); this. $ El.html (template);   

and a quote (not a complete template) from my template:

  & lt; Section & gt; & Lt; P & gt; & Lt;% = movie_symptonis% & gt; & Lt; / P & gt; & Lt; / Section & gt;   

and an example of JSON:

  [{"id": "0", "name": "some movie", "date": "Blur Blah Blah", "Gallery Images": {"Picture": "Time": "83 minutes", "Rating": "CERT / U", "Status": "Now available", "Symptom": " One.png "," image ":" two.png "," image ":" three.png "}}}   

Now I have to render some html for each gallery images Within the image, I am familiar with underscores for each method (_.each). Although I'm not sure how all this will work together, there will be any help or push in the right direction.

Thanks in advance

You can use the _.each method inside the template . It will look something like the following.

  & lt; Section & gt; & Lt; P & gt; & Lt;% = movie_symptonis% & gt; & Lt; / P & gt; & Lt;% _.each (gallery image, function (gallery image) {%> gt; & lt; img src = "& lt;% = this [galleryImage]%>" />  & Lt; / Section & gt;   

"Gallery image" is available from the Jason you are going to the template.

& lt; %% & gt; Delimiter will execute code like normal javascript.

You can use all the available compositions in JavaScript inside the template

(e.g.)

  & lt; Div & gt; & Lt;% If (Condition) {%> ... success html ... & lt;%} and {%> ... failure html .. ..tw;%}% & gt; & Lt; / Div & gt;   

You can find more about using the templates in the template. But creating a lot of logic in the template is not a good idea. But it helps to keep simple visual arguments.

Hope that helps you.

No comments:

Post a Comment