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 🙏

© 2024 – Pkg Stats / Ryan Hefner

njx

v0.2.2

Published

Nunjucks Utility

Downloads

8

Readme

njx

NPM

njx is a template helper for Nunjucks, a rich and powerful templating language for JavaScript. Nunjucks is essentially a port of Jinja2, a templating language for Python.

njx is ideal for when you just want a quick and easy way to render something using input sources and templates that are any combination of

  • strings
  • files
  • urls

You can require it as a module:

npm install njx

or use it as a command line tool:

npm install -g njx

njx command line usage

njx [options] template

template

The value for template can be a nunjucks template string, a file path, or url. To specify a file path in the current directory, use the form ./template.

options

-c --config

A file or url to a yaml or json file with supported options, such as data and template. See this yaml gist or this json gist for examples of both.

-d --data

Pipe data to njx or specify data with the -d or --data option. The value for the data option can be a json string, file path, or url. To specify a file path in the current directory, use the form ./filename.

-o --out file

Specify file to write output to. If directories in the file path don't exist, also use the -p --paths option.

-p --paths

Create intermediate directories if necessary when writing to a file.

Examples

# pipe input data to the njx command, specifying the template as a string, and append the result to a file
$ '{ "name": "World" }' | njx "Hello {{ name }}" >> hello.txt

or

# provide template, data, and output options to the njx command
njx -t "Hello {{ name }}" -d '{ "name': "World" }' -o hello.txt

or

# provide data and template options using URLs, and write to the specified output file
njx -d https://gist.githubusercontent.com/subfuzion/c77cd766397844f1fb28/raw/7f9cb47fea08e957dcb775f92bf46/data.json -t https://gist.githubusercontent.com/subfuzion/006e0d2550881956c1c9/raw/d7732488b5a9bb63830f258c9571d3f849ba494b/hello.nunjucks -o hello.txt

or

# provide a "render config" option as a url to the njx command, and append the result to a file;
# the render config provides the data and the template (in this example, they are also URLs, which njx will follow)
njx -c https://gist.githubusercontent.com/subfuzion/d0fa251f8b4ab71928e2/raw/fd33beb96524c94b395c63935c701e98f2e72b52/sample-config.yaml -o hello.txt

API

  var njx = require('njx');
  njx.render(config, callback);

Render Config

An object with at least template and data properties.

  • template - [required] a template string, filepath, or url
  • data - [required] a json string, filepath, or url
  • outfile - [optional] a filename to write the rendered result to; otherwise writes to stdout

The template and data properties interpret strings as a nunjucks template or json string, respectively, unless the string looks like a file path or url. To specify a file in the current working directory, use the form: ./filename.

Usage examples

Basic example

  var njx = require('njx');

  var config = {
    template: 'Hello {{ name }}!',
    data: { name: 'World' }
  };


  njx.render(config, function(err, result) {
    console.log(result); // Hello World
  });

Using file sources

  var njx = require('njx');

  var config = {
    template: './template.nunjucks',
    data: { name: './data.json' }
  };


  njx.render(config, function(err, result) {
    console.log(result); // Hello World
  });

Using url sources (ex: GitHub gists)

  var njx = require('njx'),
  
  var templateUrl = 'https://gist.githubusercontent.com/subfuzion/006e0d2550881956c1c9/raw/d7732488b5a9bb63830f258c9571d3f849ba494b/hello.nunjucks';
  
  var dataUrl = 'https://gist.githubusercontent.com/subfuzion/c77cd766397844f1fb28/raw/7f9c526ae145e6fb47fea08e957dcb775f92bf46/data.json'

  var config = {
    template: templateUrl,
    data: { name: dataUrl }
  };


  njx.render(config, function(err, result) {
    console.log(result); // Hello World
  });