How to prevent jsrender from creating multiple template instances when model is an array

StackOverflow https://stackoverflow.com/questions/20362914

  •  28-08-2022
  •  | 
  •  

Domanda

I am passing an array object as a model to a jsrender template. I would like to render the model using:

{{for ~data}}
<div>{{>Name}}</div>
{{/for}}

However, the default behavior seems to be to render the template for each item of the array automatically. Any way to prevent that that doesn't require changing my data model?

È stato utile?

Soluzione

You can simply wrap your model in an array. Here is a sample that does that:

Sample: Helpers, and layout templates (www.jsviews.com/#samples/jsr/helpers)

var html = $("#movieTemplate").render(
    // Wrap movies array in an array, to render as a layout with header and footer
    [movies],
    helpers
)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top