/* Okay, so, I'm not so fond of the JavaDoc syntax,as I can not really recall it off the top of my head.Instead, I'll just give a brief overview of how this functionworks. It expects a single argument, an object which containsdata about replacements to make. Honestly, it could be givennothing, so long as you have at least one function definedsomewhere for it to use as a replacer. Anyways, the objectshould have at least one value in it: delim. delim is a string,either 1 or 2 characters. If it is 1 character, then that singlecharacter serves as the delimiter for values to be replaced;if it is 2 characters, then those two are split up and the 1stis used as ldelim, the 2nd as rdelim. Alternatively, you can passldelim and rdelim separately.Anyways. Another thing you will want to do is set tmfunPrefixto the string your template functions begin with. After this thingdoes a pass through and replaces everything that it finds in RepObj,it then makes a swoop through finding any left values to replace, andif it does it uses that name to determine the name of a template functionto use to find the value, i.e., if you have [random] in there, and youhave a function named, let's say, tm_random, assuming tm_ is yourfunction prefix, it will replace [random] with whatever that functionmay return. So, that's about it. Enjoy, I suppose. */var tmfunPrefix = "tm_" // Prefix for template functions.String.prototype.template = function(RepObj) { eval = evalfix; // ignore this, it has to do with the engine this is based around. temp = this.toString(); // Get the string. if(RepObj["delim"] == undefined) { //Checking to see if the delimiter is in there. ldelim = (RepObj["ldelim"]==undefined?"":RepObj["ldelim"]); //Set ldelim perhaps? rdelim = (RepObj["rdelim"]==undefined?"":RepObj["rdelim"]); //" " rdelim " " if(ldelim != "" && rdelim == "") { //If ldelim is set but rdelim isn't, rdelim = ldelim; // make them the same thing. } else if(rdelim != "" && ldelim == "") { //vice versa. ldelim = rdelim; } else { //Neither are set. Let's make them the % sign. ldelim = rdelim = "%" } } else { delim = RepObj["delim"]; if(delim.length==1) { // Only one character? ldelim = rdelim = delim.charAt(0); //then they both will be it. } else { //more than one? ldelim = delim.charAt(0); //Character 1 is ldelim, rdelim = delim.charAt(1); //Character 2 is rdelim. } } for(x in RepObj) { //Enumerate all of the members of RepObj if(x=="ldelim" || x == "rdelim" || x == "delim") continue; //If it happens to be one of the delims, just skip it. while(temp.indexOf(ldelim+x+rdelim) != -1) { //Is the template string still in there? temp = temp.replace(ldelim+x+rdelim,RepObj[x],"g"); //replace it with the necessary value. } } /*Okay this part was annoying. I was originally going to handle it with regex. Guess what didn't want to work for me? Good job. So, I broke it down to just basic text handling. Probably a tiny bit more efficient anyways. */ changes = []; tempVar = ""; delimCount = 0; addToTempBool = false; iterations = 0; //This was to protect it from iterating too many times. It honestly did for some reason. maxIt = temp.length; //max amount of times it can iterate. for(i = 0; i < temp.length; i++) { //dig through the characters. if(iterations > maxIt) break; //If it has gone through all of the characters and is somehow still iterating, exit. buchstabe = temp.charAt(i); //Get current character if(buchstabe==ldelim) { //If it's the left delimiter tempVar = ""; //Refresh the temporary var addToTempBool=true; //Say it's okay to start adding to it. } else if(buchstabe==ldelim && addToTempBool) { //If we encounter another ldelim, ignore it but pop it to the stack, so to speak. delimCount++; //keep count of how many. } else if(buchstabe==rdelim && delimCount == 0) { //If we're at the rdelim and that's the absolute last delim (this is prone to error) changes.push(tempVar); //Push it on the stack. addtoTempBool = false; } else if(buchstabe == rdelim && delimCount > 0) { //If we hit rdelim and there has been a previous ldelim besides the first. delimCount--; //decrement that delimcount continue; } else if(addToTempBool) { //If we are to add a letter to the temp tempVar += buchstabe; //Then add it. } else { } iterations++; } iterations = 0; maxIt = changes.length; for (i = 0; i < changes.length; i++) { if(iterations > maxIt) break; //Again. rep = changes[i]; //Get current expected template postfix eS = tmfunPrefix+rep+"();"; //Set up the eval string if((repES = eval(eS))) { //check if the function exists repString = ldelim+rep+rdelim; //Set up replace string. temp = temp.replace(repString,repES,"g"); //Do those replacements, yo. } iterations++; } /* This here was what it originally was, I have no clue why, but it refuses to work. I'm keeping it here for some reason or another. maybe I will figure out why in the future. while((m = temp.match(new RegExp(ldelim+".*?"+rdelim,"i")))) { temp = temp.replace(ldelim+"(.*?)"+redelim,function($1) { if(eval("tm_"+$1+"()") != 0) { return eval("tm_"+$1+"()"); } },"gi"); } }*/ return temp; //return the modified variable. Do it.} /* To-Do: -Add support for just one delimiter, preferably on the left side only. Right side would do good though. Other than that, I can't think of anything. Hit me up sometime if you can. AIM: Sir mXe-mXe*//*Also, example usage:"<var1>".template({"delim":"<>","var1":"Example usage"])would produce the string"Example usage"It possesses more power than one might think... perhaps.*/