Email: longsu2010atyeahdotnet anyone who knows about JohnResig's JavaScriptMicro-Templating knows its conciseness and the amount of code to implement. In the dojo/string module, dojo provides a very concise template replacement function substitute. Here is an example... S
Email: longsu2010 at yeah dot net
Anyone who knows John Resig's JavaScript Micro-Templating knows its simplicity and the amount of code to be implemented is small.
In the dojo/string module, dojo provides a very concise template replacement function substitute.
Here is an example.
Require (["dojo/string"], function (string ){
Var html = "File '$ {name}' is not found in directory '$ {info. dir }'.";
Var s = string. substitute (html, {name: "foo.html", info: {dir: "/temp "}});
Console. log (s );
}); The output result is:
File 'foo.html 'is not found in directory'/temp '.
We can see that the first parameter of substitute is the template string, and the second parameter is the replaced data.
Usage 2:
Var html = "File '$ {0}' is not found in directory '$ {1 }'."
Console. log (string. substitute (html, ["foo.html", "/temp"]);
Output result:
File 'foo.html 'is not found in directory'/temp '.
It can be seen that the second parameter can be an array, which must be represented in the form of a lower mark in the template.
The substitute method signature is: substitute (/* String */template,/* Object | Array */map,/* Function? */Transform,/* Object? */ThisObject)
Usage 3:
Require (["dojo/string"], function (string ){
Var fmts = {
Fmt: function (value ){
Console. log ("fmt:", value );
Return "-" + value + "-";
},
Transform: function (value, key ){
Console. log ("transform:", value, key );
Return key + ":" + value;
}
}
Var html = "File '$ {name: fmt}' is not found in directory '$ {info. dir }'.";
Var s = string. substitute (html, {name: "foo.html", info: {dir: "/temp" }}, fmts. transform, fmts );
Console. log (s );
}); The output result is:
Fmt: foo.html
Transform: -foo.html-
Transform:/temp info. dir
File 'name: -foo.html-'is not found in directory 'info. dir:/temp '.
Note:
1. Use the fmt function to format the attribute $ {name: fmt} above.
2. The third parameter of substitute is a conversion function (Optional). This function is called every time you replace a template variable (the parameter is the template variable value and template variable name ), if this parameter is not available, use the transform function in the fourth parameter. If the fourth parameter does not contain transform, do not perform this operation.
3. The fourth parameter of substitute is an object (optional, global by default ). When the template engine encounters a $ {name: fmt} template variable, it searches for the fmt function in the object. If the third parameter is not provided and the object contains a function named transform, use this transform as the third parameter.
In addition to the substitute function, the dojo/string module also provides the following functions:
Reg (/* String */str,/* Integer */num): copy the String of the first parameter to the second parameter.
Pad (/* String */text,/* Integer */size,/* String? */Ch,/* Boolean? */End): Fill text with ch to the length of size. If end is true, it is filled after text; otherwise, it is filled before.
Trim: trim is the trim in the String prototype. If trim is not in the String prototype (not supported by the js engine), the dojo implementation function is the same as trim.