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

Javascript uses initialization data to assemble template implementation code_javascript skills

WBOY
Release: 2016-05-16 18:16:18
Original
844 people have browsed it

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;
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template