# @allmarkedup/nunjucks-with

> A scoped context block extension for Nunjucks

Latest version **0.1.2** (published 2017-10-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @allmarkedup/nunjucks-with
pnpm add @allmarkedup/nunjucks-with
yarn add @allmarkedup/nunjucks-with
bun add @allmarkedup/nunjucks-with
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2017-10-23 |
| First published | 2017-10-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Mark Perkins |
| Maintainers | allmarkedup |
| Keywords | nunjucks, context, scope |

## Links

- npm: https://www.npmjs.com/package/@allmarkedup/nunjucks-with
- Repository: https://github.com/allmarkedup/nunjucks-with
- Issues: https://github.com/allmarkedup/nunjucks-with/issues
- npm.io page: https://npm.io/package/@allmarkedup/nunjucks-with

## Dependencies (2)

- [chai](https://npm.io/package/chai.md) ^4.1.2
- [nunjucks](https://npm.io/package/nunjucks.md) ^3.0.1

## Recent versions

- 0.1.2 (latest) — 2017-10-23
- 0.1.1 — 2017-10-23
- 0.1.0 — 2017-10-23

## README

# Nunjucks 'with'

A scoped context block extension for Nunjucks.

## Installation

```
npm i @allmarkedup/nunjucks-with
```

## Usage

`nunjucks-with` works similarly to the `with` statement in JavaScript

```js
const nunjucks = require('nunjucks');
const WithExtension = require('@allmarkedup/nunjucks-with');
const env = new nunjucks.Environment(/* loaders etc... */);

env.addExtension('WithExtension', new WithExtension());

const tpl = `
{{ level }}
{% with nested %}
{{ level }}
{% endwith %}
`;

const output = env.renderString(tpl, {
  level: 'one',
  nested: {
    level: 'two'
  }
});

console.log(output);
/* 
one
two
*/

```

You can also pass in objects defined on the fly:

```nunjucks
{% set myScope = 'global' %}
{{ scope }} <!-- outputs 'global' -->

{% with {myScope: 'block'} %}
{{ myScope }} <!-- outputs 'block' -->
{% endwith %}
```

Nunjucks doesn't let you pass in scope when `include`-ing templates, so `nunjucks-with` is pretty handy for isolating scope for your includes to avoid variable name clashes:

```nunjucks
<!-- views/main.tpl -->
<p>{{ text }}<p>
{% with button %}{% include 'button.tpl' %}{% endwith %}

<!-- views/button.tpl -->
<button>{{ text }}</button>
```

```js
const output = env.render('main.tpl', {
  text: 'you should to this',
  button: {
    text: 'click here'
  }
});

/*
<p>you should to this</p>
<button>click here</button>
 */
```

## Credits:

`nunjucks-with` is based on code by @vinnyrose in a comment on the Nujucks issue tracker: https://github.com/mozilla/nunjucks/issues/722#issuecomment-281722126

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