查文檔,使用模板引擎的 API http://aui./art-template/zh-cn/docs/
案例
tpl.html
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>{{ title }}</title></head><body><p>大家好,我叫:{{ name }}</p><p>我今年 {{ age }} 歲了</p><h1>我來自 {{ province }}</h1><p>我喜歡:{{each hobbies}} {{ $value }} {{/each}}</p><script>var foo ='{{ title }}'</script></body></html>
app.js
var template =require('art-template')var fs =require('fs')// 這里不是瀏覽器// template('script 標(biāo)簽 id', {對(duì)象})
fs.readFile('./tpl.html',function(err, data){if(err){return console.log('讀取文件失敗了')}// 默認(rèn)讀取到的 data 是二進(jìn)制數(shù)據(jù)// 而模板引擎的 render 方法需要接收的是字符串// 所以我們?cè)谶@里需要把 data 二進(jìn)制數(shù)據(jù)轉(zhuǎn)為 字符串 才可以給模板引擎使用var ret = template.render(data.toString(),{
name:'李里',
age:18,
province:'北京市',
hobbies:['寫代碼','吃飯','打游戲'],
title:'個(gè)人信息'})
console.log(ret)})