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

gulp-nunjucks-api

v0.9.1

Published

Render Nunjucks templates with data, custom filters, custom context functions and options for other Nunjucks API features.

Downloads

398

Readme

gulp-nunjucks-api

npm version npm downloads dependencies Status

Render Nunjucks templates with data, custom filters, custom context functions and options for other Nunjucks API features.

Install

Install with npm

npm install --save-dev gulp-nunjucks-api

Example

var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-api');

gulp.task('default', function () {
  return gulp.src('src/templates/*.html')
    .pipe(nunjucksRender({
		  src: 'src/templates',
      data: require('./global-data.json'),
      filters: require('./global-filters.js'),
      functions: require('./global-functions.js')
		}))
		.pipe(gulp.dest('dist'));
});

Example with gulp data

var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-api');
var data = require('gulp-data');

function getDataForFile(file){
  return {
    example: 'data loaded for ' + file.relative
  };
}

gulp.task('default', function () {
	return gulp.src('src/templates/*.html')
	  .pipe(data(getDataForFile))
		.pipe(nunjucksRender({
      src: 'src/templates/'
    }))
		.pipe(gulp.dest('dist'));
});

API

gulp-nunjucks-api(options)

Renders source templates using the given options to configure the Nunjucks API with custom data, extensions, filters and contextual functions.

Same options as nunjucks.configure():

  • watch (default: false) reload templates when they are changed.
  • express an express app that nunjucks should install to.
  • autoescape (default: false) controls if output with dangerous characters are escaped automatically. See Autoescaping.
  • tags (default: see nunjucks syntax) defines the syntax for nunjucks tags. See Customizing Syntax.

With the following additional options:

  • extension (default: ".html") String. File extension to output. Pass 'inherit' to use the extension of the input file.
  • src (default: undefined) String or Array. Search path(s) for nunjucks.configure().
  • data (default: {}) Ojbect. Global data merged into the Nunjucks render context.
  • errors (default: true) Boolean. Whether to emit errors to gulp or not. Set to false to let the gulp task continue on errors. See also: the verbose option.
  • extensions (default: {}) Object. Global extensions added to the Nunjucks environment. See Custom Tags.
  • filters (default: {}) Object. Global filter functions added to the Nunjucks environment. See Custom Filters.
  • functions (default: {}) Object. Global functions merged into the Nunjucks render context.
  • globals (default: undefined) Object. A single object which provides data, extensions, filters and functions objects instead of setting each of these options separately. The separate global options are merged into this base object.
  • locals (default: undefined) Boolean or String. When true, enables loading of local template context data and functions from files that match the following default pattern: "<filename>.+(js|json)". When a glob pattern string is given, the directory containing a given template will be searched using the pattern. Data and functions from all matched files are merged into the render context. Note that the token <filename> will be replaced with a given template's file name including extension. Use the <filename_noext> token instead in a custom pattern to target the file name without extension.
  • verbose (default: false) Boolean. When true, detailed operational data is logged to the console.

Render with data example

nunjucksRender({
  data: {css_path: 'http://company.com/css/'}
});

For the following template

<link rel="stylesheet" href="{{ css_path }}test.css" />

Would render

<link rel="stylesheet" href="http://company.com/css/test.css" />

Watch mode

Nunjucks' watch feature, which is normally enabled by default, is disabled by
default for gulp. Pass watch: true to enable it:

nunjucksRender({
  src: './source',
  watch: true
});

License

MIT © Devoptix LLC

Shout-outs

Carlos G. Limardo who wrote gulp-nunjucks-render which I am forking in order to update Nunjucks and do other stuff.

Sindre Sorhus who wrote the original gulp-nunjucks for precompiling Nunjucks templates.