Prototype Template Object Learning _prototype

Source: Internet
Author: User
Copy Code code as follows:

var Template = class.create ({
Initialization method
Initialize:function (template, pattern) {
This.template = Template.tostring ();
This.pattern = Pattern | | Template.pattern;
},

Formatting method, if from the Java point of view, actually called format is better:)
Evaluate:function (object) {
Check to see if the Totemplatereplacements method is defined, so call the
In the entire prototype framework, only the hash object defines this method
If (Object && object.isfunction (object.totemplatereplacements))
Object = Object.totemplatereplacements ();

The gsub here is the method inside the string object, which can be simply thought of as replacing all the matching pattern parts in the string.
Return this.template.gsub (This.pattern, function (match) {
MATCH[0] is the entire string of matching Template.pattern
Match[1] is a character in front of the matching string
MATCH[2] is a part of the expression that matches ${var}
MATCH[3] is the ' var ' section of the ' #{var} ' expression

If object is null, all ${var} expressions are replaced with '
if (object = = null) return (match[1] + ');

Gets the previous character of a matching expression
var before = match[1] | | '';
If the previous string is ' \ ', the matching expression is returned directly without replacing
if (before = = ' \ ') return match[2];

var ctx = object, expr = match[3];
This regular expression is like checking whether Var is a valid name, and not understanding the meaning of this regular expression at the moment?
var pattern =/^ ([^.[] +|\[((?:.*? [^\\])?) \])(\.| \[|$)/;
Match = pattern.exec (expr);
Returns the previous character directly if Var does not meet the requirement
if (match = = null) return before;
Replace the ' #{var} ' expression section one by one
while (match!= null) {
What do you mean, I don't know what this check means?
var comp = match[1].startswith (' [')? Match[2].gsub (' \\\\] ', '] '): match[1];
CTX = Ctx[comp];
if (null = = CTX | | ' = = match[3] ') break;
Expr = expr.substring (' [' = = match[3]? match[1].length:match[0].length);
Match = pattern.exec (expr);
}
Returns the result of the replacement, ' #{var} ' ==> ' value '
Return before + String.interpret (CTX);
});
}
});
The default template matching regular expression, like #{var}, is very similar to the El expression in the JSP
Template.pattern =/(^|.| \r|\n) (#\{(. *?) \})/;

The above basically put the Evaluate method, some places do not see how to understand, those regular expression is too difficult to understand ... Who knows, tell me?

Here's a look at the example:
Copy Code code as follows:

var myTemplate = new Template (' The TV show #{title} is created by #{author}. ');

var show = {title: ' The Simpsons ', Author: ' Matt Groening ', Network: ' FOX '};

Mytemplate.evaluate (show);
-> The TV show the Simpsons is created by Matt Groening.

Copy Code code as follows:

var t = new Template (' in #{lang} We also to use the \\#{variable} syntax for templates. ');
var data = {lang: ' Ruby ', variable: ' (not used) '}; T.evaluate (data);
-> in Ruby We also use the #{variable} syntax for templates.

Copy Code code as follows:

Custom Match mode
var syntax =/(^|.| \r|\n) (\<%=\s* (\w+) \s*%\>)/;

Matches symbols like ' <%= field%> '
var t = new Template (' <div>name: <b><%= Name%></b>, Age: <b><%=age%></b>< /div> ', syntax);

T.evaluate ({name: ' John Smith ', age:26});
-> <div>name: <b>john smith</b>, Age: <b>26</b></div>


Copy Code code as follows:

var conversion1 = {from: ' meters ', to: ' Feet ', factor:3.28};
var conversion2 = {from: ' Kilojoules ', to: ' BTUs ', factor:0.9478};
var conversion3 = {from: ' Megabytes ', to: ' Gigabytes ', factor:1024};

var Templ = new Template (' Multiply by #{factor} to convert from #{from} to #{to}.

[Conversion1, Conversion2, Conversion3].each (function (CONV) {templ.evaluate (conv);});
//-> Multiply by 3.28 to convert from meters to feet.
//-> Multiply by 0.9478 to convert from kilojoules to BTUs.
//-> Multiply by 1024 to convert from megabytes to gigabytes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.