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

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

  •  28-08-2022
  •  | 
  •  

Pergunta

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?

Foi útil?

Solução

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
)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top