
Product
Announcing Socket Certified Patches: One-Click Fixes for Vulnerable Dependencies
A safer, faster way to eliminate vulnerabilities without updating dependencies
@allmarkedup/nunjucks-with
Advanced tools
A scoped context block extension for Nunjucks.
npm i @allmarkedup/nunjucks-with
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>
*/
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
FAQs
A scoped context block extension for Nunjucks
We found that @allmarkedup/nunjucks-with demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Product
A safer, faster way to eliminate vulnerabilities without updating dependencies

Product
Reachability analysis for Ruby is now in beta, helping teams identify which vulnerabilities are truly exploitable in their applications.

Research
/Security News
Malicious npm packages use Adspect cloaking and fake CAPTCHAs to fingerprint visitors and redirect victims to crypto-themed scam sites.