nunjucks-includeData
Let nunjucks template Include data from JSON file
How to install?
npm install nunjucks-includeData
How to use it?
Config
var nunjucks = ;var njIncludeData = ; var templatesDir = ...; // Your template foldervar nunjucksEnv = nunjucks; // Config your nunjucks with the templateDirnjIncludeData; // 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 namespacefile
: path is relative to the templatesDir (see the config section above), expression is acceptedclean
: 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 namespacefile
: 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 namespacefile
: 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 namespacefile
: 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 namespacefile
: 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 namespacefile
: 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 namespacefile
: 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 namespacefile
: 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 namespacefile
: path is relative to the templatesDir (see the config section above), expression is accepted
Example 1 - Basic
JSON data file: templatesDir
/data/user.json
Template:
% includeData 'data/user.json' 'data/user.json' as user% Hello firstName lastNameHi userfirstName userlastName
Display output:
Hello Bill GateHi Bill Gate
Example 2 - Basic clean
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
Template:
% includeData 'data/user.json' as user 'data/user2.json' as user 'data/user.json' as player 'data/user2.json' as player clean% Hello userfirstName userlastName useremail !Hi playerfirstName playerlastName playeremail !
Display output:
Hello Bill Gate tim.cook@gmail.com!Hi Bill Gate !
Example 3 - Inject to root
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
JSON data file: templatesDir
/data/user3.json
Template:
% includeData 'data/user3.json' as player% Morning playerfirstName playerlastName playeremail ! % includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi playerfirstName playerlastName playeremail !
Display output:
Morning Peter Pan peter.pan@gmail.com!Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate peter.pan@gmail.com!
Example 4 - Inject to root (clean)
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
JSON data file: templatesDir
/data/user3.json
Template:
% includeData 'data/user3.json' as player% Morning playerfirstName playerlastName playeremail ! % includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi playerfirstName playerlastName playeremail !
Display output:
Morning Peter Pan peter.pan@gmail.com!Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate !
Example 5 - Inject to here
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
Template:
% includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi userplayerfirstName userplayerlastName userplayeremail !
Display output:
Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate peter.pan@gmail.com!
Example 6 - Inject to here (clean)
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
Template:
% includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi userplayerfirstName userplayerlastName userplayeremail !
Display output:
Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate !
Example 7 - Inject to root (1st element of array)
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
JSON data file: templatesDir
/data/user3.json
Template:
% includeData 'data/user3.json' as player% Morning playerfirstName playerlastName playeremail ! % includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi playerfirstName playerlastName playeremail !
Display output:
Morning Peter Pan peter.pan@gmail.com!Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate peter.pan@gmail.com!
Example 8 - Inject to root (clean) (1st element of array)
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
JSON data file: templatesDir
/data/user3.json
Template:
% includeData 'data/user3.json' as player% Morning playerfirstName playerlastName playeremail ! % includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi playerfirstName playerlastName playeremail !
Display output:
Morning Peter Pan peter.pan@gmail.com!Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate !
Example 9 - Inject to here (1st element of array)
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
Template:
% includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi userplayerfirstName userplayerlastName userplayeremail !
Display output:
Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate peter.pan@gmail.com!
Example 10 - Inject to here (clean) (1st element of array)
JSON data file: templatesDir
/data/user.json
JSON data file: templatesDir
/data/user2.json
Template:
% includeData 'data/user.json' as user% Hello userfirstName userlastName useremail !Hi userplayerfirstName userplayerlastName userplayeremail !
Display output:
Hello Tim Cook tim.cook@gmail.com!Hi Bill Gate !