PugNode 模板引擎
Pug(原名 Jade,因商標(biāo)問(wèn)題改名)是一個(gè)強(qiáng)大、優(yōu)雅、功能豐富的 Node.js 模板引擎。
Pug 的一般渲染過(guò)程很簡(jiǎn)單,pug.compile()會(huì)將 Pug 源碼編譯成 JavaScript 函數(shù),該 JavaScript 函數(shù)將數(shù)據(jù)對(duì)象locals作為參數(shù),調(diào)用該結(jié)果函數(shù),將返回與數(shù)據(jù)一起呈現(xiàn)的 HTML 字符串。
可以重復(fù)使用已編譯的函數(shù),并使用不同的數(shù)據(jù)集調(diào)用該函數(shù)。
//- template.pug
p #{name}'s Pug source code!
const pug = require('pug');
// Compile the source code
const compiledFunction = pug.compileFile('template.pug');
// Render a set of data
console.log(compiledFunction({
name: 'Timothy'
}));
// "<p>Timothy's Pug source code!</p>"
// Render another set of data
console.log(compiledFunction({
name: 'Forbes'
}));
// "<p>Forbes's Pug source code!</p>"
Pug 還提供了pug.render()將編譯和渲染結(jié)合在一起的一系列功能。但是,每次render調(diào)用時(shí)都會(huì)重新編譯模板函數(shù),這可能會(huì)影響性能。
const pug = require('pug');
// Compile template.pug, and render a set of data
console.log(pug.renderFile('template.pug', {
name: 'Timothy'
}));
// "<p>Timothy's Pug source code!</p>"評(píng)論
圖片
表情
