Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling icanhaz templates from a remote server

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?

like image 400
user961794 Avatar asked Oct 21 '22 10:10

user961794


1 Answers

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 :)

like image 123
Simon Adcock Avatar answered Oct 27 '22 20:10

Simon Adcock