Opened 12 years ago

Closed 12 years ago

#3248 closed enhancement (fixed)

general-purpose templateString replacement

Reported by: guest Owned by: anonymous
Priority: high Milestone: 0.9beta
Component: Dijit Version: 0.9
Keywords: Cc:
Blocked By: Blocking:

Description

The templateString replacement routine is embedded in dojo.widget.DomWidget?.buildFromTemplate, but could be made more useful if it were a separate routine that took as a parameter a string and returned a string with the substituted parameters.

Change History (2)

comment:1 Changed 12 years ago by guest

code (almost verbatim from DomWidget?):

  templateSubstitution: function(str) {
      var rv = str;
      matches = str.match(/${([^}]+)}/g);
      if (matches) {
	  var hash = this.strings || {};
	  for (var key in dojo.widget.defaultStrings) {
	      if (dojo.lang.isUndefined(hash[key])) {
		  hash[key] = dojo.widget.defaultStrings[key];
	      }
	  }
	  for (var i = 0; i < matches.length; i++) {
	      var key = matches[i];
	      key = key.substring(2, key.length - 1);
	      var kval = (key.substring(0, 5) == "this.") ? 
		  dojo.lang.getObjPathValue(key.substring(5), this) : 
		  hash[key];
	      var value;
	      if ((kval) || (dojo.lang.isString(kval))) {
		  value = new String((dojo.lang.isFunction(kval)) ? 
				     kval.call(this, key, str) : kval);
		  while (value.indexOf(""") > -1) {
		      value = value.replace(""", "&quot;");
		  }
		  rv = rv.replace(matches[i], value);
	      }
	  }
      }

      return rv;
  }

comment:2 Changed 12 years ago by bill

Component: GeneralDijit
Milestone: 0.9beta
Resolution: fixed
Status: newclosed

In Dijit we do replacement via dojo.string.substitute()

Note: See TracTickets for help on using tickets.