HenrikJoreteg/ICanHaz.js

Able to specify template name and data through function.

aidanmorgan opened this issue · 2 comments

Hi,

First of all I love ICH, it works really nicely.

I'm sorry if I missed this somewhere from scanning through the code but I think it would be really helpful to be able to call a function and give it the name of the template and the data that I wish to use.

I am trying to write some custom helper code for something I am working on, and I have the template name and the url to load data from embedded in the DOM as such:

   <div id="item-container" data-url="/ajax/something" data-template="itemTemplate"/>

When I want to load the contents of the div, I can access the data-url and data-template attributes and use jQuery to load the data, however I have no way to get ICH to render the template for me.. Ideally what I need is an ich.render(template, data); function that I could call so I can programatically use the templates.

Cheers,

Aidan

Just use jQuery to run an ich.addTemplate on the data that's returned from your query. This is very similar to the json example on the main page, unless I'm misunderstanding. You're basically talking about doing this:

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

Right? But you're just specifying some of the parameters as data attributes that you presumably read then would put into the above function (making the url and final template name dynamic).

Right, you can use ich.addTemplate(templateName, template) to set these after the fact. Let me know if i misunderstood.