HenrikJoreteg/ICanHaz.js

template retrieval without hardcoding template name

Closed this issue · 2 comments

When using AJAX to load my templates, I'm using a format similar to the documentation... my server sends back a JSON object with a "name" parameter and a "template" parameter, and I use ich.addTemplate(name, template).

However, from what I can tell, the only way to a retrieve a template is to use ich.#templateName#(data)... so how can I retrieve a template if the name is stored in a variable and isn't hardcoded? The only solution I've come up with so far is using eval(), but that's not an approach I would like to take.

One of my fellow developers came up with this solution. Might be a useful addition.

self.getTemplate = function(name, data, raw){
    return self[name](data,raw);
};

I'm not 100% convinced I understand the question, but if you're asking what I think you're asking, you can simply to this:

var templateName = 'user';
ich[templateName]();

No magic necessary :)

In js the following are identical:

foo.bar;
foo['bar'];

If I misunderstood, you please re-open this issue.