API リファレンス
このページでは、JavaScript API を使用して Pug をレンダリングする方法について詳しく説明します。
ヒント
Pug は Web ブラウザのコンソールで使用できます! このページに記載されている Pug の API を試してみるには、次のように入力してください。
pug.render('p Hello world!');
オプション
すべての API メソッドは、次のオプションを受け入れます。
- filename: string
- コンパイルされるファイルの名前。例外で使用され、相対的な
include
とextend
に必要です。デフォルトは'Pug'
です。 - basedir: string
- すべての絶対インクルードのルートディレクトリ。
- doctype: string
doctype
がテンプレートの一部として指定されていない場合は、ここで指定できます。自己終了タグを取得し、ブール属性のミラーリングを削除するのに役立つ場合があります。詳細については、doctype のドキュメントを参照してください。- pretty: boolean | string
- [非推奨] 結果の HTML に空白を追加して、人間が読みやすくするために、インデントとして
' '
を使用します。文字列が指定された場合は、代わりにそれがインデントとして使用されます(例:'\t'
)。このオプションの使用は強くお勧めしません。空白の解釈とレンダリング方法が変更されるため、テンプレートに微妙なバグが発生することが多いため、この機能は削除される予定です。デフォルトはfalse
です。 - filters: object
- カスタムフィルターのハッシュテーブル。デフォルトは
undefined
です。 - self: boolean
- ローカル変数を保持するために
self
名前空間を使用します。コンパイル速度は向上しますが、ローカルオブジェクトのプロパティにアクセスするには、variable
と書く代わりにself.variable
と書く必要があります。デフォルトはfalse
です。 - debug: boolean
true
に設定すると、トークンと関数本体が標準出力にログ出力されます。- compileDebug: boolean
true
に設定すると、エラーメッセージを改善するために、コンパイルされたテンプレートに関数ソースが含まれます(開発時に役立つ場合があります)。本番モードで Express と共に使用しない限り、デフォルトで有効になっています。- globals: Array<string>
- テンプレート内でアクセスできるようにグローバル名のリストを追加します。
- cache: boolean
true
に設定すると、コンパイルされた関数がキャッシュされます。filename
はキャッシュキーとして設定する必要があります。render
関数にのみ適用されます。デフォルトはfalse
です。- inlineRuntimeFunctions: boolean
- 共有バージョンから `require` する代わりに、ランタイム関数をインライン化します。`compileClient` 関数の場合、デフォルトは `true` です(ランタイムを含める必要がないため)。他のすべてのコンパイルまたはレンダリングタイプの場合、デフォルトは `false` です。
- name: string
- テンプレート関数の名前。 `compileClient` 関数にのみ適用されます。デフォルトは `'template'` です。
メソッド
pug.compile(source, ?options)
Pug テンプレートを関数にコンパイルします。この関数は、異なるローカル変数で複数回レンダリングできます。
- source: string
- コンパイルする Pug テンプレートのソース
- options: ?options
- オプションオブジェクト
- returns: function
- ローカル変数を含むオブジェクトから HTML を生成する関数
var pug = require('pug');
// Compile a function
var fn = pug.compile('string of pug', options);
// Render the function
var html = fn(locals);
// => '<string>of pug</string>'
pug.compileFile(path, ?options)
ファイルから Pug テンプレートを関数にコンパイルします。この関数は、異なるローカル変数で複数回レンダリングできます。
- path: string
- Pug ファイルへのパス
- options: ?options
- オプションオブジェクト
- returns: function
- ローカル変数を含むオブジェクトから HTML を生成する関数
var pug = require('pug');
// Compile a function
var fn = pug.compileFile('path to pug file', options);
// Render the function
var html = fn(locals);
// => '<string>of pug</string>'
pug.compileClient(source, ?options)
Pug テンプレートを、クライアント側で使用できる JavaScript の文字列にコンパイルします。
- source: string
- コンパイルする Pug テンプレート
- options: ?options
- オプションオブジェクト
- returns: string
- 関数を表す JavaScript の文字列
var pug = require('pug');
// Compile a function
var fn = pug.compileClient('string of pug', options);
// Render the function
var html = fn(locals);
// => 'function template(locals) { return "<string>of pug</string>"; }'
pug.compileClientWithDependenciesTracked(source, ?options)
compileClient
と同じですが、このメソッドは次の形式のオブジェクトを返します。
{
'body': 'function (locals) {...}',
'dependencies': ['filename.pug']
}
Pug ファイルの変更の監視などを実装するために `dependencies` が必要な場合にのみ、このメソッドを使用する必要があります。
pug.compileFileClient(path, ?options)
Pug テンプレートファイルを、クライアント側で使用できる JavaScript の文字列にコンパイルします。
- path: string
- Pug ファイルへのパス
- options: ?options
- オプションオブジェクト
- options.name: string
- オプションオブジェクトに `.name` プロパティを渡すと、クライアント側のテンプレート関数の名前として使用されます。
- returns: string
- JavaScript 関数本体。
まず、テンプレートファイルです。
h1 This is a Pug template
h2 By #{author}
次に、Pug ファイルを関数文字列にコンパイルします。
var fs = require('fs');
var pug = require('pug');
// Compile the template to a function string
var jsFunctionString = pug.compileFileClient('/path/to/pugFile.pug', {name: "fancyTemplateFun"});
// Maybe you want to compile all of your templates to a templates.js file and serve it to the client
fs.writeFileSync("templates.js", jsFunctionString);
出力関数文字列は次のようになります(`templates.js` に書き込まれます)。
function fancyTemplateFun(locals) {
var buf = [];
var pug_mixins = {};
var pug_interp;
var locals_for_with = (locals || {});
(function (author) {
buf.push("<h1>This is a Pug template</h1><h2>By "
+ (pug.escape((pug_interp = author) == null ? '' : pug_interp))
+ "</h2>");
}.call(this, "author" in locals_for_with ?
locals_for_with.author : typeof author !== "undefined" ?
author : undefined)
);
return buf.join("");
}
<html>
<head>
<script src="/templates.js"></script>
</head>
<body>
<h1>This is one fancy template.</h1>
<script type="text/javascript">
var html = window.fancyTemplateFun({author: "enlore"});
var div = document.createElement("div");
div.innerHTML = html;
document.body.appendChild(div);
</script>
</body>
</html>
pug.render(source, ?options, ?callback)
- source: string
- レンダリングする Pug テンプレートのソース
- options: ?options
- オプションオブジェクト。ローカル変数オブジェクトとしても使用されます。
- callback: ?function
- レンダリング結果を受け取る Node.js スタイルのコールバック。**このコールバックは同期的に呼び出されます。**
- returns: string
- 結果の HTML 文字列
var pug = require('pug');
var html = pug.render('string of pug', options);
// => '<string>of pug</string>'
pug.renderFile(path, ?options, ?callback)
- path: string
- レンダリングする Pug ファイルへのパス
- options: ?options
- オプションオブジェクト。ローカル変数オブジェクトとしても使用されます。
- callback: ?function
- レンダリング結果を受け取る Node.js スタイルのコールバック。**このコールバックは同期的に呼び出されます。**
- returns: string
- 結果の HTML 文字列
var pug = require('pug');
var html = pug.renderFile('path/to/file.pug', options);
// ...
プロパティ
pug.filters
カスタムフィルターのハッシュテーブル。
このオブジェクトは `filters` オプションと同じセマンティクスを持ちますが、すべての Pug コンパイルにグローバルに適用されます。`pug.filters` と `options.filters` の両方にフィルターが存在する場合、`filters` オプションが優先されます。
非推奨
このプロパティは、 `filters` オプションを支持して非推奨になりました。