H
H
Hellas2017-01-13 18:12:24
JavaScript
Hellas, 2017-01-13 18:12:24

Template or what?

Now I'm using a template engine to build HTML according to json data.
When updating, the entire page, together with the header, is rendered on the server side, and when switching to other pages, data about the requested page is taken by ajax and the rendered page is inserted into the current HTML.
But what if we need to re-render a separate fragment, for example:

{
  people: [
    "Yehuda Katz",
    "Alan Johnson",
    "Charles Jolley"
  ]
}

handlebars:
<ul class="people_list">
  {{#each people}}
    <li>{{this}}</li>
  {{/each}}
</ul>

=>
<ul class="people_list">
  <li>Yehuda Katz</li>
  <li>Alan Johnson</li>
  <li>Charles Jolley</li>
</ul>

And then the client sends a request in which the server returns the following data:
{
  people: [
    "Katz Yehuda",
    "Jolley Charles",
    "Johnson Alan",
    "Yehuda Katz",
    "Charles Jolley"
  ]
}

Need to re-render {{#each people}} with new data. How to do it?
In the question - How to re-render a block in handlebars? they said that for such purposes it is necessary to use frameworks (Backbone, Emberjs, Angular). But are there really no template engines that are capable of this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Taratin, 2017-01-13
@Hellas

But are there really no template engines that are capable of this?

It will no longer be called a template engine. => There are none.
If you need something small, then try riot.js

D
Dima Gashko, 2017-01-13
@Mnemonist

Well, any js template will do.
be it built into some framework, or underscore, or lodash ( https://learn.javascript.ru/template-lodash) , or even a separate simple template like:
ejohn.org/blog/javascript-micro-templating

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question