New OpenLayers.String.format function to Format a string given a string

template and some context -- to be used within the SLD framework. Developed
by Andreas and Sr. Schaub. Thanks, guys!


git-svn-id: http://svn.openlayers.org/trunk/openlayers@5317 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2007-12-01 13:20:59 +00:00
parent 10cf5bfefc
commit 76fa0da3b7
2 changed files with 72 additions and 1 deletions

View File

@@ -87,7 +87,44 @@ OpenLayers.String = {
camelizedString += s.charAt(0).toUpperCase() + s.substring(1);
}
return camelizedString;
},
/**
* APIFunction: OpenLayers.String.format
* Given a string with tokens in the form ${token}, return a string
* with tokens replaced with properties from the given context
* object. Represent a literal "${" by doubling it, e.g. "${${".
*
* Parameters:
* template - {String} A string with tokens to be replaced. A template
* has the form "literal ${token}" where the token will be replaced
* by the value of context["token"].
* context - {Object} An optional object with properties corresponding
* to the tokens in the format string. If no context is sent, the
* window object will be used.
*
* Returns:
* {String} A string with tokens replaced from the context object.
*/
format: function(template, context) {
if(!context) {
context = window;
}
var tokens = template.split("${");
var item, last;
for(var i=1; i<tokens.length; i++) {
item = tokens[i];
last = item.indexOf("}");
if(last > 0) {
tokens[i] = context[item.substring(0, last)] +
item.substring(++last);
} else {
tokens[i] = "${" + item;
}
}
return tokens.join("");
}
};
if (!String.prototype.startsWith) {