String.interpolate - Prototype JavaScript 框架

Xunxin Prototype API

interpolate
1.6

interpolate(object[, pattern]) -> string

将字符串看作一个模板,并使用 object 的属性填充它。

相当于调用 Templateevaluate 方法。

样例

"#{animals} on a #{transport}".interpolate({ animals:"Pigs", transport: "Surfboard" }); 
//-> "Pigs on a Surfboard" 
var syntax = /(^|.|\r|\n)(\<%=\s*(\w+)\s*%\>)/;
//匹配类似于 '<%= field %>' 这样的符号
var html = '<div>Name: <b><%= name %></b>, Age: <b><%=age%></b></div>';
html.interpolate({ name: 'John Smith', age: 26 }, syntax); 
// -> <div>Name: <b>John Smith</b>, Age: <b>26</b></div>