Socket
Book a DemoInstallSign in
Socket

@allmarkedup/nunjucks-with

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@allmarkedup/nunjucks-with

A scoped context block extension for Nunjucks

0.1.2
latest
Source
npmnpm
Version published
Weekly downloads
8
14.29%
Maintainers
1
Weekly downloads
 
Created
Source

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

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:

{% 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:

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

<!-- views/button.tpl -->
<button>{{ text }}</button>
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

Keywords

nunjucks

FAQs

Package last updated on 23 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.