模板引擎-1-基础概念和是使用小案例

一,基础概念

模板引擎是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之后,就会得到:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值