node.js
的模块—art-template
模块引擎:是一个第三方模块,需要先下载。npm install art-template
使用template方法返回一个html
文件,第一个参数为模板路径,这里的文件后缀是art
,第二个参数是一个对象,可以将数据写入到html
文件里。
1.将数据输出到模板里。
const template = require('art-template');
const path = require('path');
const views = path.join(__dirname,'views','index.art');
const html =template(views,{
name: '张三',
age : 20
})
console.log(html);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 标准语法 -->
{{name}}
{{age}}
<!-- 原生语法 -->
<%= name%>
<%= age%>
</body>
</html>
输出结果:
2.若要输出html标签,默认模板不会解析标签,而会转义后输出。
const template = require('art-template');
const path = require('path');
const views = path.join(__dirname,'views','index.art');
const html =template(views,{
name: '张三',
age : 20,
content: '<h1>我是标题</h1>'
})
console.log(html);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 标准语法 -->
{{name}}
{{age}}
{{@ content}}
<!-- 原生语法 -->
<%= name%>
<%= age%>
<%- content%>
</body>
</html>