npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nunjucks-includeData

v0.0.9

Published

Let nunjucks template Include data from JSON file

Readme

nunjucks-includeData

Let nunjucks template Include data from JSON file

#How to install?

npm install nunjucks-includeData

#How to use it? Config

var nunjucks = require('nunjucks');
var njIncludeData = require('nunjucks-includeData');

var templatesDir = ...; // Your template folder
var nunjucksEnv = nunjucks.configure( templatesDir, { ... } );  // Config your nunjucks with the templateDir
njIncludeData.install(nunjucksEnv);  // Init the extension with the nunjucks environment

#Tag Syntax

{% includeData <file> [as <namespace>] [clean] [, <file> [as <namespace>] [clean], ...] %}
  • namespace: if no namespace supplied, then it will be the global namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted
  • clean: clean the namespace before read in the file, this option has no effect on the global namespace

#Json file keyword ##Inject a json file to the root scope

__injectToRoot_as_[namespace]: <file>
  • namespace: if no namespace supplied, then it will be the global namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Inject a json file to the root scope (clean)

__injectToRoot_asClean_[namespace]: <file>

Empty the namespace before json injection. Produce the same result as __injectToRoot_as_ if no namespace is provided.

  • namespace: if no namespace supplied, then it will be the global namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Inject a json file to current scope

__injectToHere_as_[namespace]: <file>
  • namespace: if no namespace supplied, then it will be the current namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Inject a json file to current scope (clean)

__injectToHere_as_[namespace]: <file>

Empty the namespace before json injection. Produce the same result as __injectToHere_as_ if no namespace is provided.

  • namespace: if no namespace supplied, then it will be the current namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Inject a json file to the root scope, if the json file contains an array, then insert the first element rather than whole array

__injectArray0ToRoot_as_[namespace]: <file>
  • namespace: if no namespace supplied, then it will be the global namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Inject a json file to the root scope (clean), if the json file contains an array, then insert the first element rather than whole array

__injectArray0ToRoot_asClean_[namespace]: <file>

Empty the namespace before json injection. Produce the same result as __injectArray0ToRoot_as_ if no namespace is provided.

  • namespace: if no namespace supplied, then it will be the global namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Inject a json file to current scope, if the json file contains an array, then insert the first element rather than whole array

__injectArray0ToHere_as_[namespace]: <file>
  • namespace: if no namespace supplied, then it will be the current namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Inject a json file to current scope (clean), if the json file contains an array, then insert the first element rather than whole array

__injectArray0ToHere_as_[namespace]: <file>

Empty the namespace before json injection. Produce the same result as __injectArray0ToHere_as_ if no namespace is provided.

  • namespace: if no namespace supplied, then it will be the current namespace
  • file: path is relative to the templatesDir (see the config section above), expression is accepted

##Example 1 - Basic JSON data file: templatesDir/data/user.json

{
	"firstName": "Bill",
	"lastName": "Gate"
}

Template:

{% includeData
  'data/user.json',
  'data/user.json' as user
%}

Hello {{ firstName }} {{ lastName}}
Hi {{ user.firstName }} {{ user.lastName}}

Display output:

Hello Bill Gate
Hi Bill Gate

##Example 2 - Basic clean JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]"
}

JSON data file: templatesDir/data/user2.json

{
	"firstName": "Bill",
	"lastName": "Gate"
}

Template:

{% includeData
  'data/user.json' as user,
  'data/user2.json' as user,
  'data/user.json' as player,
  'data/user2.json' as player clean
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

Display output:

Hello Bill Gate [email protected]!
Hi Bill Gate !

##Example 3 - Inject to root JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"__injectToRoot_as_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

{
	"firstName": "Bill",
	"lastName": "Gate"
}

JSON data file: templatesDir/data/user3.json

{
	"firstName": "Peter",
	"lastName": "Pan",
	"email": "[email protected]"
}

Template:

{% includeData
  'data/user3.json' as player
%}

Morning {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

Display output:

Morning Peter Pan [email protected]!
Hello Tim Cook [email protected]!
Hi Bill Gate [email protected]!

##Example 4 - Inject to root (clean) JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"__injectToRoot_asClean_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

{
	"firstName": "Bill",
	"lastName": "Gate"
}

JSON data file: templatesDir/data/user3.json

{
	"firstName": "Peter",
	"lastName": "Pan",
	"email": "[email protected]"
}

Template:

{% includeData
  'data/user3.json' as player
%}

Morning {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

Display output:

Morning Peter Pan [email protected]!
Hello Tim Cook [email protected]!
Hi Bill Gate !

##Example 5 - Inject to here JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"player": {
		"email": "[email protected]"
	},
	"__injectToHere_as_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

{
	"firstName": "Bill",
	"lastName": "Gate"
}

Template:

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ user.player.firstName }} {{ user.player.lastName}} {{ user.player.email }}!

Display output:

Hello Tim Cook [email protected]!
Hi Bill Gate [email protected]!

##Example 6 - Inject to here (clean) JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"player": {
		"email": "[email protected]"
	},
	"__injectToHere_asClean_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

{
	"firstName": "Bill",
	"lastName": "Gate"
}

Template:

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ user.player.firstName }} {{ user.player.lastName}} {{ user.player.email }}!

Display output:

Hello Tim Cook [email protected]!
Hi Bill Gate !

##Example 7 - Inject to root (1st element of array) JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"__injectArray0ToRoot_as_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

[{
	"firstName": "Bill",
	"lastName": "Gate"
}, {
	"firstName": "Tim",
	"lastName": "Cook"
}]

JSON data file: templatesDir/data/user3.json

{
	"firstName": "Peter",
	"lastName": "Pan",
	"email": "[email protected]"
}

Template:

{% includeData
  'data/user3.json' as player
%}

Morning {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

Display output:

Morning Peter Pan [email protected]!
Hello Tim Cook [email protected]!
Hi Bill Gate [email protected]!

##Example 8 - Inject to root (clean) (1st element of array) JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"__injectArray0ToRoot_asClean_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

[{
	"firstName": "Bill",
	"lastName": "Gate"
}, {
	"firstName": "Time",
	"lastName": "Cook"
}]

JSON data file: templatesDir/data/user3.json

{
	"firstName": "Peter",
	"lastName": "Pan",
	"email": "[email protected]"
}

Template:

{% includeData
  'data/user3.json' as player
%}

Morning {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ player.firstName }} {{ player.lastName}} {{ player.email }}!

Display output:

Morning Peter Pan [email protected]!
Hello Tim Cook [email protected]!
Hi Bill Gate !

##Example 9 - Inject to here (1st element of array) JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"player": {
		"email": "[email protected]"
	},
	"__injectArray0ToHere_as_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

[{
	"firstName": "Bill",
	"lastName": "Gate"
}, {
	"firstName": "Tim",
	"lastName": "Cook"
}]

Template:

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ user.player.firstName }} {{ user.player.lastName}} {{ user.player.email }}!

Display output:

Hello Tim Cook [email protected]!
Hi Bill Gate [email protected]!

##Example 10 - Inject to here (clean) (1st element of array) JSON data file: templatesDir/data/user.json

{
	"firstName": "Tim",
	"lastName": "Cook",
	"email": "[email protected]",
	"player": {
		"email": "[email protected]"
	},
	"__injectArray0ToHere_asClean_player": "data/user2.json"
}

JSON data file: templatesDir/data/user2.json

[{
	"firstName": "Bill",
	"lastName": "Gate"
}, {
	"firstName": "Time",
	"lastName": "Cook"
}]

Template:

{% includeData
  'data/user.json' as user
%}

Hello {{ user.firstName }} {{ user.lastName}} {{ user.email }}!
Hi {{ user.player.firstName }} {{ user.player.lastName}} {{ user.player.email }}!

Display output:

Hello Tim Cook [email protected]!
Hi Bill Gate !