consttemplate='My name is ${name}, age ${age}, I am a ${job.name}'constemployee= {
name: 'fatfish',
age: 100,
job: {
name: 'front end development'
}
}
constrenderStr=template.render(employee)
// What is the output string?console.log(renderStr) // 'My name is fatfish, age 100, I am a front end development'
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
什么是模板引擎?
你一定用过nunjucks之类的模板引擎,题型和它的功能很像,请跟着我一起举个例子。
nunjucks.configure({ autoescape: true })
consttemplate='My name is {{name}}, age {{age}}, I am a {{job.name}}'constemployee= {
name: 'fatfish',
age: 100,
job: {
name: 'front end development'
}
}
constrenderStr=nunjucks.renderString(template, employee)
console.log(renderStr) // My name is fatfish, age 100, I am a front end development
String.prototype.render=function (obj) {
consttemplate=thisconstvariableRegex=/\$\{([^${}]+)\}/gtemplate.replace(variableRegex, ($0, variable) => {
console.log(variable)
})
}
consttemplate='My name is ${name}, age ${age}, I am a ${job.name}'template.render()
String.prototype.render=function (obj) {
consttemplate=thisconstvariableRegex=/\$\{([^${}]+)\}/gconstgetVariableValue= (variable) => {
// [ 'name' ]、[ 'age' ]、[ 'job', 'name' ]variable=variable.split('.')
letvariableValue=obj// For example, if we want to get the value of job.name, we will go through the following steps// Initialization: variableValue = { name: 'fatfish', age: 100, job: { name: "front end development" } }// first loop: variableValue = { name: "front end development" }// Second loop: variableValue = 'front end development'// Third loop: finished, return 'front end development'while (variable.length) {
variableValue=variableValue[ variable.shift() ]
}
returnvariableValue
}
constrenderStr=template.replace(variableRegex, ($0, variable) => {
returngetVariableValue(variable)
})
returnrenderStr
}
consttemplate='My name is ${name}, age ${age}, I am a ${job.name}'constemployee= {
name: 'fatfish',
age: 100,
job: {
name: 'front end development'
}
}
constrenderStr=template.render(employee)
console.log(renderStr)
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
我们通过正则表达式实现了一个简单的模板引擎,请小伙伴们为自己打气。
解决方案 2:eval
朋友们,让我们回顾一下es6中模板字符串的基本用法。
constname='fatfish'constage=100constjob= {
name: 'front end development'
}
constrenderString=`My name is ${name}, age ${age}, I am a ${job.name}`console.log(renderString)
String.prototype.render=function (obj) {
consttemplate=this// var { name, age, job } = objeval(`var {${Object.keys(obj).join(',')}} = obj`)
// `My name is ${name}, age ${age}, I am a ${job.name}`constrenderStr=eval('`'+template+'`')
returnrenderStr
}
consttemplate='My name is ${name}, age ${age}, I am a ${job.name}'constemployee= {
name: 'fatfish',
age: 100,
job: {
name: 'front end development'
}
}
constrenderStr=template.render(employee)
String.prototype.render=function (obj) {
with(obj) {
returneval('`'+this+'`')
}
}
consttemplate='My name is ${name}, age ${age}, I am a ${job.name}'constemployee= {
name: 'fatfish',
age: 100,
job: {
name: 'front end development'
}
}
constrenderStr=template.render(employee)
console.log(renderStr)