dojo.string.substitute
| parameter | type | description |
|---|---|---|
| template | String | a string with expressions in the form ${key} to be replaced or
${key:format} which specifies a format function. keys are case-sensitive. |
| map | Object|Array | hash to search for substitutions |
| transform | Function | Optional. a function to process all parameters before substitution takes place, e.g. mylib.encodeXML |
| thisObject | Object | Optional. where to look for optional format function; default to the global namespace |
Usage
Examples
Example 1
// returns "File 'foo.html' is not found in directory '/temp'." dojo.string.substitute( "File '${0}' is not found in directory '${1}'.", ["foo.html","/temp"] ); // also returns "File 'foo.html' is not found in directory '/temp'." dojo.string.substitute( "File '${name}' is not found in directory '${info.dir}'.", { name: "foo.html", info: { dir: "/temp" } } );
Example 2
use a transform function to modify the values:
// returns "file 'foo.html' is not found in directory '/temp'." dojo.string.substitute( "${0} is not found in ${1}.", ["foo.html","/temp"], function(str){ // try to figure out the type var prefix = (str.charAt(0) == "/") ? "directory": "file"; return prefix + " '" + str + "'"; } );
Example 3
use a formatter
// returns "thinger -- howdy" dojo.string.substitute( "${0:postfix}", ["thinger"], null, { postfix: function(value, key){ return value + " -- howdy"; } } );
Example 4
Substitutes two expressions in a string from an Array or Object
// returns "File 'foo.html' is not found in directory '/temp'." // by providing substitution data in an Array dojo.string.substitute( "File '${0}' is not found in directory '${1}'.", ["foo.html","/temp"] ); // also returns "File 'foo.html' is not found in directory '/temp'." // but provides substitution data in an Object structure. Dotted // notation may be used to traverse the structure. dojo.string.substitute( "File '${name}' is not found in directory '${info.dir}'.", { name: "foo.html", info: { dir: "/temp" } } );
Example 5
Use a transform function to modify the values:
// returns "file 'foo.html' is not found in directory '/temp'." dojo.string.substitute( "${0} is not found in ${1}.", ["foo.html","/temp"], function(str){ // try to figure out the type var prefix = (str.charAt(0) == "/") ? "directory": "file"; return prefix + " '" + str + "'"; } );
Example 6
Use a formatter
// returns "thinger -- howdy" dojo.string.substitute( "${0:postfix}", ["thinger"], null, { postfix: function(value, key){ return value + " -- howdy"; } } );