一,基础概念
模板引擎是node的第三方模块。
让开发者以更加友好的方式拼接字符串,使项目的代码更加清晰,更加易于维护。
二,我们采用art-template模板引擎
1,下载:使用npm install art-template
2,使用const template=require(‘art-template’)引入模板引擎
3,告诉模板引擎要拼接的数据和模板在哪?const html =template(‘模板路径’,数据)
新建的模板和连接:index.art
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
{{name}}
{{age}}
</body>
</html>
app.js:
//导入模板引擎,返回值是一个方法
const template=require('art-template')
//拼接字符串的第三方模块
const path=require('path')
//template 方法是用来拼接字符串的
//参数1:模板路径,绝对路径
///参数2:要在模板中显示的数据 它是对象类型
//返回值是拼接好的字符串
const views = path.join(__dirname,'views','index.art')
const html=template(views,{
name:'张三',
age:20
})
console.log(html)
执行app.js之后,就会得到: