◆ 模板引擎
模板引擎是第三方模塊。讓開發者以更加友好的方式拼接字符串,使項目代碼更加清晰、更加易於維護。
◆ art-template模板引擎:
- 在命令行工具中使用 npm install art-template 命令進行下載
- 使用const template = require(‘art-template’)引入模板引擎
- 告訴模板引擎要拼接的數據和模板在哪 const html = template(‘模板路徑’, 數據);
- 使用模板語法告訴模板引擎,模板與數據應該如何進行拼接
示例
// 導入模板引擎模塊
const template = require('art-template');
// 將特定模板與特定數據進行拼接
const html = template('./views/index.art',{
data: {
name: '張三',
age: 20
}
});
<div>
<span>{{data.name}}</span>
<span>{{data.age}}</span>
</div>