# pug-attrs

> Generate code for Pug attributes

Latest version **3.0.0** (published 2020-05-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install pug-attrs
pnpm add pug-attrs
yarn add pug-attrs
bun add pug-attrs
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: no vulnerabilities; popular repo.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2020-05-25 |
| First published | 2015-12-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 7.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 21826 |
| Author | Forbes Lindesay |
| Maintainers | forbeslindesay, pug-bot, timothygu |
| Keywords | pug |

## Links

- npm: https://www.npmjs.com/package/pug-attrs
- Repository: https://github.com/pugjs/pug/tree/master/packages/pug-attrs
- npm.io page: https://npm.io/package/pug-attrs

## Dependencies (3)

- [pug-runtime](https://npm.io/package/pug-runtime.md) ^3.0.0
- [js-stringify](https://npm.io/package/js-stringify.md) ^1.0.2
- [constantinople](https://npm.io/package/constantinople.md) ^4.0.1

## Recent versions

- 3.0.0 (latest) — 2020-05-25
- 3.0.0-canary-2 (canary) — 2020-05-25
- 3.0.0-canary-1 — 2020-05-24
- 2.0.4 — 2019-06-18
- 2.0.3 — 2018-03-06
- 2.0.2 — 2017-01-24
- 2.0.1 — 2016-01-08
- 0.0.1 — 2015-12-12
- 0.0.0 — 2015-12-12

## README

# pug-attrs

Generate code for Pug attributes

[![Build Status](https://img.shields.io/travis/pugjs/pug-attrs/master.svg)](https://travis-ci.org/pugjs/pug-attrs)
[![Dependencies Status](https://david-dm.org/pugjs/pug/status.svg?path=packages/pug-attrs)](https://david-dm.org/pugjs/pug?path=packages/pug-attrs)
[![NPM version](https://img.shields.io/npm/v/pug-attrs.svg)](https://www.npmjs.org/package/pug-attrs)

## Installation

    npm install pug-attrs

## Usage

```js
var compileAttrs = require('pug-attrs');
```

### `compileAttrs(attrs, options)`

Compile `attrs` to a JavaScript string that evaluates to the attributes in the desired format.

`options` MUST include the following properties:

- `terse`: whether or not to use HTML5-style terse boolean attributes
- `runtime`: callback that takes a runtime function name and returns the source code that will evaluate to that function at runtime
- `format`: output format; must be `html` or `object`

`attrs` is an array of attributes, with each attribute having the form of `{ name, val, mustEscape }`. `val` represents a JavaScript string that evaluates to the value of the attribute, either statically or dynamically.

```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}
];
var result, finalResult;

// HTML MODE
result = compileAttrs(attrs, {
  terse:   true,
  format:  'html',
  runtime: function (name) { return 'pugRuntime.' + name; }
});
//=> '" foo=\\"bar\\"" + pugRuntime.attr("baz", getBaz(), true, true) + " quux"'

finalResult = Function('pugRuntime, getBaz',
  'return (' + result + ');'
);
finalResult(pugRuntime, getBaz);
// => ' foo="bar" baz="baz&lt;&gt;" quux'

// OBJECT MODE
result = compileAttrs(attrs, {
  terse:   true,
  format:  'object',
  runtime: function (name) { return 'pugRuntime.' + name; }
});
//=> '{"foo": "bar","baz": pugRuntime.escape(getBaz()),"quux": true}'

finalResult = Function('pugRuntime, getBaz',
  'return (' + result + ');'
);
finalResult(pugRuntime, getBaz);
//=> { foo: 'bar', baz: 'baz&lt;&gt;', quux: true }
```

## License

  MIT

---
_Source: https://npm.io/package/pug-attrs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
