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

rosid-handler-njk

v8.0.0

Published

Load EJS templates and render them

Downloads

219

Readme

rosid-handler-njk

Travis Build Status Coverage Status Dependencies Greenkeeper badge

A function that loads a Nunjucks template and transforms it to HTML.

Install

npm install rosid-handler-njk

Usage

API

const handler = require('rosid-handler-njk')

handler('index.njk').then((data) => {})
handler('index.njk', { optimize: true }).then((data) => {})
handler('index.njk', { data: { key: 'value' } }).then((data) => {})
handler('index.njk', { data: 'data.json' }).then((data) => {})

Rosid

Add the following object to your rosidfile.json, rosidfile.js or routes array. rosid-handler-njk will transform all matching Nunjucks files in your source folder to HTML.

{
	"name"    : "NJK",
	"path"    : "[^_]*.{html,njk}*",
	"handler" : "rosid-handler-njk"
}
<!-- index.njk -->
<h1>Hello {{ 'World' }}</h1>
<!-- index.html (output) -->
<h1>Hello World</h1>

Parameters

  • filePath {String} Path to file.
  • opts {?Object} Options.
    • optimize {?Boolean} - Optimize output. Defaults to false.
    • data {?Object|String} - Data used to render the template. Defaults to {}.
    • localOverwrites {?Boolean} - Enable or disable custom data per file. Defaults to true.
    • prepend {?String} - String that will be placed in front of the content of filePath. Defaults to ''.
    • append {?String} - String that will be placed at the end of the content of filePath. Defaults to ''.
    • src {?String} - Path base for injects with the inject tag. Defaults to the current working directory.
    • shy {?RegExp} - What to replace when using the shy filter. Defaults to /\|/g.

Returns

  • {Promise<String|Buffer>} The transformed file content.

Miscellaneous

Inject tag

rosid-handler-njk adds a custom Nunjucks extension you can use in your templates. The Nunjucks tag inject allows you to include other Nunjucks files with custom data. This feature is currently not part of Nunjucks.

{% inject 'button.njk' %}
{% inject 'button.njk', { color: 'purple', text: 'Button' } %}

The path to the file is always relative to the current working directory or to the path specified in opts.src. This behavior is different to Nunjucks's build-in include tag, where the path is relative to the initial file.

<!-- src/index.njk -->
{% inject 'src/includes/a.njk' %}
{% include 'includes/a.njk' %}

<!-- src/includes/a.njk -->
{% inject 'src/includes/b.njk' %}
{% include 'includes/b.njk' %}

<!-- src/includes/b.njk -->
{{ 'This is a file' }}

Shy filter

rosid-handler-njk adds a custom filter that replaces | with &shy; and indicates that the string should not be auto escaped by Nunjucks (similar to the safe filter).

{{ 'Long head|lines are awe|some' | shy }}

You can customize the behaviour of the filter by passing a custom shy regexp to the module.

Data

The data in opts.data will be used to render your template. opts.data can either be an object (the data) or a string (path to data file). rosid-handler-njk tries to require the path when a string is specified instead of an object. The path must be absolute or relative to the current working directory.

Custom data per file

Create a file with the name filename.data.json or filename.data.js along your filename.njk to add or overwrite data from opts.data. You can disable this behaviour with the localOverwrites option.

Environment

rosid-handler-njk passes a variable called environment to your template. environment is prod when opts.optimize is true and dev when opts.optimize is false.