I have the following code which I am using for making a simple template engine.The html page is as follows:-
<ns tmp="red"></ns>
<ns tmp="blue"></ns>
On the other hand I have a JavaScript as in a JQuery Plugin which reads the tags and then gets the attribute of tmp
now I want to receive the value of the string and then convert it to a function so as to give a call to a predefined value inside the object but the conversion of the string to function is not working. I referred some questions in Stack overflow but wasn't useful. Then JQuery code I have mentioned below.
(function($){
/*Collection of the template data*/
var k=template();
/*This retrieves all the custom tags and gets the template
property to point to.*/
var templateArray=$('ns');
templateArray.each(function(){
var template=$(this).attr('tmp');
var funcName=window[template]();//This does not work
alert(l());
});
})(jQuery);
function template(){
var t={
blue:function(){
return "Hello";
},
red:function(){
return "ff";
}
};
return t;
}
Please suggest how to get along this. I have this on fiddle too. feel free to edit so that I would be able to call the function inside the object in some way.Thanks Fiddle Link FIDDLE LINK
window["red"]andwindow["blue"];but there are no methods defined on the window that are named red and blue.