GrauKatze 05f70669c9 Сконфигурирована и подготовлена база для проекта | 2 yıl önce | |
---|---|---|
.. | ||
lib | 2 yıl önce | |
CHANGELOG.md | 2 yıl önce | |
LICENSE | 2 yıl önce | |
README.md | 2 yıl önce | |
build.js | 2 yıl önce | |
index.js | 2 yıl önce | |
package.json | 2 yıl önce | |
wrap.js | 2 yıl önce |
The runtime components for the pug templating language
npm install pug-runtime
You can call runtime methods directly using runtime.method
. This is particularly useful when compiling to deal with things that are already known at compile time.
var runtime = require('pug-runtime');
assert(runtime.attr('foo', 'bar', true, true) === ' foo="bar"');
You can also build a string with a given list of functions available as pug_method
by calling build(arrayOfMethods)
. This is useful for inlining runtime functions within the compiled templates.
var build = require('pug-runtime/build');
var src = build(['attr']);
var attr = Function('', src + ';return pug_attr;')();
assert(attr('foo', 'bar', true, true) === ' foo="bar"');
When testing code compiled for the browser in Node.js, it is necessary to make the runtime available. To do so, one can use require('pug-runtime/wrap')
:
var pug = require('pug');
var wrap = require('pug-runtime/wrap');
var pugSrc = 'p= content';
// By default compileClient automatically embeds the needed runtime functions,
// rendering this module useless.
var compiledCode = pug.compileClient(pugSrc, {
externalRuntime: true
});
//=> 'function template (locals) { ... pug.escape() ... }'
var templateFunc = wrap(compiledCode);
templateFunc({content: 'Hey!'});
//=> '<p>Hey!</p>'
// Change template function name to 'heyTemplate'
compiledCode = pug.compileClient(pugSrc, {
externalRuntime: true,
name: 'heyTemplate'
});
//=> 'function heyTemplate (locals) { ... }'
templateFunc = wrap(compiledCode, 'heyTemplate');
templateFunc({content: 'Hey!'});
//=> '<p>Hey!</p>'
MIT