The compileAttrs function supports two distinct output modes via the format option:
- HTML Mode (
format: 'html'): Produces a JavaScript string that, when evaluated, returns a string of HTML attributes (e.g., foo="bar" quux). This is used when generating raw HTML templates. - Object Mode (
format: 'object'): Produces a JavaScript string that, when evaluated, returns a plain JavaScript object representing the attributes (e.g., { foo: 'bar', quux: true }). This is useful when working with virtual DOMs or object-based attribute systems.
In both modes, the runtime callback is used to map internal Pug runtime calls (like escaping) to your specific runtime implementation.
```js
var compileAttrs = require('pug-attrs');
var pugRuntime = require('pug-runtime');
function getBaz () { return 'baz<>'; }
var attrs = [
{name: 'foo', val: '"bar"', mustEscape: true },
{name: 'baz', val: 'getBaz()', mustEscape: true },
{name: 'quux', val: true, mustEscape: false}
];
// HTML MODE
var htmlResultCode = compileAttrs(attrs, {
terse: true,
format: 'html',
runtime: function (name) { return 'pugRuntime.' + name; }
});
// htmlResultCode evaluates to: '