Home  >  Article  >  Web Front-end  >  Javascript uses initialization data to assemble template implementation code_javascript skills

Javascript uses initialization data to assemble template implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:16:18824browse

var list = [{id:1, name:"czone", age:21}, {id:2, name:'czonechan', age:21}];
var template ='

$name$age
';

Implement a general method to assemble the template using initialization data.

Copy code The code is as follows:

function displayTemplate(list,template){
var result="";
for(var i=0,l=list.length;ivar temp=template;
for(var k in list[i])
{
var reg=new RegExp("\$" k,"g");
temp=temp.replace(reg,function(w){
return list[i][k] ;
})
}
result =temp;
}
return result;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn