nodejs(九)art-template模板引擎
阿新 • • 發佈:2018-12-16
nodejs中可以使用模板引擎,其中art-template既可以用在前端,也可以用在伺服器端,首先看一下前端用法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- 作迴圈
{{each hobbies}} {{ $value }} {{/each}} -->
<!--瀏覽器中引入這個檔案-- >
<script src="../node_modules/art-template/lib/template-web.js"></script>
<script type="text/template" id="tpl">
hello {{name}}
</script>
<script>
var ret = template('tpl', {
name: 'Jack'
})
console.log(ret)
</script>
</body>
< /html>
後端使用首先引入依賴
npm install art-template -S
var template = require('art-template')
render函式有兩個引數,第一個是輸入的字串,第二個是替換的物件的值
var ret = template.render('hello {{name}}', {
name: 'jack'
})
console.log(ret)
執行結果