Nodejs資料之模板引擎

◆ 模板引擎

模板引擎是第三方模塊。讓開發者以更加友好的方式拼接字符串,使項目代碼更加清晰、更加易於維護。

在這裏插入圖片描述

◆ art-template模板引擎:
  1. 在命令行工具中使用 npm install art-template 命令進行下載
  2. 使用const template = require(‘art-template’)引入模板引擎
  3. 告訴模板引擎要拼接的數據和模板在哪 const html = template(‘模板路徑’, 數據);
  4. 使用模板語法告訴模板引擎,模板與數據應該如何進行拼接

示例

 // 導入模板引擎模塊
 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>
發佈了293 篇原創文章 · 獲贊 6 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章