2
votes

I wonder if I can use several JSONs when I'm rendering a page with dust.js.

For example, I am on the client side. I download my JSONs (JSON1 and JSON2) and my dust template (dustTemplate). I compile and load my template. After I would render my page with the 2 JSONs.

For now I do :dust.render("template" , JSON, function(err, out) {...}); But it uses one JSON, is there a way to use 2 or more JSONs ?

Thank you

3

3 Answers

2
votes

You can definitely use multiple JSON payloads on a single page (and they don't need to arrive at the same time). Each payload needs its own call to dust.render, though they can potentially use the same template, if you want.

For instance:

async('GET', url1, function(data) {
  if (data) {
    data = JSON.parse(data);
    dust.render('template1', data, function(err, out) {
      if (typeof out === 'string') {
        container1.innerHTML = out;
      }
    });
  }
});

async('GET', url2, function(data) {
  if (data) {
    data = JSON.parse(data);
    dust.render('template2', data, function(err, out) {
      if (typeof out === 'string') {
        container2.innerHTML = out;
      }
    });
  }
});
0
votes

To use 2 JSON or more for rendering one page. We can merge the JSONs with jQuery

var jsonMerge = $.extend({}, JSON1, JSON2);
var templateCompiled = dust.compile(dustTemplate, "template");
dust.loadSource(templateCompiled );
dust.render("template" , jsonMerge , function(err, out) {...});
0
votes

The solution pointed by @Mitch simple and efficient. Other way to do this without using jquery is http://jsfiddle.net/yXx5L/4/

Using dust.makeBase to aggregate the jsons of the page to a single context stack, upon which dust can look for the keys.