Pulling icanhaz templates from a remote server

346 Views Asked by At

The icanhaz documentation uses this as an example as how to pull ich templates from a remote server.

$.getJSON('/myserver/templates.json', function (templates) {
    $.each(templates, function (template) {
         ich.addTemplate(template.name, template.template);
    });
});

However, the documentation doesn't really tell you what the file on the remote server has to look like. Anyone have any ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

Your templates JSON object may look like this:

{
   "templates": {"name": "optionTemplate",
                 "template": "{{#options}}<option value='{{value}}'>{{display}}</option>{{/options}}"
                }
}

This will define a template for options in a select box.

You can add the template using the code you specified (actually I tweaked it slightly as I couldn't get it to work as specified):

$.getJSON('templates.json', function (templates) {
    $.each(templates, function () {
        ich.addTemplate(this.name, this.template);
    });
});

//now call getJSON on your input data

$.getJSON('options.json', function (data) {
    var optionElements = ich.optionTemplate(data);
    $('#selectBox').append(optionElements);
}

For clarity, here is what options.json contains:

{
  "options": [
             { "value": "optionValue",
               "display": "optionDisplay"
             },
             { "value": "optionValue2",
               "display": "optionDisplay2"
             }]
}

Do let me know how you get on :)