pugはnpmから入手できます:
$ npm install pug
概要
Pugの一般的なレンダリングプロセスは単純です。pug.compile ()は、データ・オブジェクト(「“locals
” と呼ばれる)を引数として取るJavaScript関数にPugソース・コードをコンパイルします。その結果得られた関数を呼び出すと、voilà! はデータをレンダリングしたHTMLの文字列を返します。
コンパイルされた関数は再利用することができ、異なるデータセットで呼び出すことができます。
//- 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.render ()関数ファミリも提供されています。ただし、レンダリングが呼び出されるたびにテンプレート関数が再コンパイルされるため、パフォーマンスに影響する可能性があります。または、レンダリングでキャッシュオプションを使用して、コンパイルされた関数を内部キャッシュに自動的に格納することもできます。
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>"
コメント