Code Monkey home page Code Monkey logo

njk's Introduction

njk

GitHub Workflow Status npm version node-current GitHub

Render nunjucks templates with markdown and front-matter
generate ( and minify ) html based on nunjucks templates, markdown, front-matter and json/yml data


About

njk is no longer maintained. It was created in 2017 as a command line tool to help me migrate from Jekyll. It served it's purpose well for me, but since then, I've migrated almost all of my sites to use other tools. And for all those repositories using it as a library along with nunjucks and express, 🫡

Install

Install with npm

npm install --dev njk

or with yarn

yarn add --dev njk

Usage

njk <files|dirs|globs> [options]

Command Line Flags

  • -V prints version
  • -h or --help prints help text
  • -v or --verbose includes additional logging
  • -b or --block wraps a content block by default. This is convenient when you you want to extend just one block. This helps you avoid writing extends tag in child template
  • -c or --clean uses clean urls (urls with forward slash) for output files.
  • -q or --quiet silences the output until any error occurs.
  • -w or --watch runs everything in watch mode. HTML is not minified in this mode.

Command Line Options

  • -d or --data pass either json file path of yaml directory path containing data.
  • -t or --template pass template directories (nunjucks searchPaths). Multiple template directories can be passed, separated by comma ,
  • -o or --out pass output directory

File Specific Options

Following options can be configured through front-matter of individual files.

  • layout parent layout/template to use for rendering a file. This inserts a extends tag automatically.
  • block Wraps a content block around a page. If enabled, an empty content block is required in parent template where content will be inserted.
  • clean Uses clean urls while writing files. For example file.html will be written as file/index.html

Contributing

You can help improving njk in following ways -

  • Found a bug, create an issue with relevant information.
  • Want a feature to be added, A pull request is always welcome.

Some Examples

1. Rendering a template using block flag and layout option in front matter

We can avoid wrapping extends tags and overriding block tags, If we need to inject single block in parent template.

Step 1

Consider we have a nunjucks template with single block.

default.njk

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Page Title</title>
</head>
<body>
  {% block content %}{% endblock %}
  <!-- The block name content is important -->
</body>
</html>

and a simple html page

index.html

---
layout: default
---
<header>
<h1>On Laughing</h1>
</header>
<main>
<p>A laugh draws a lot of painful lines.</p>
</main>
<footer>
<small>Copyright &copy; Creator Inc.</small>
</footer>

Step 2

Now, Let's run njk.

njk index.html -b

The current directory will be used for templates.

Result

The result will be something like

dist/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Page Title</title>
  </head>
  <body>
    <header>
      <h1>On Laughing</h1>
    </header>
    <main>
      <p>A laugh draws a lot of painful lines.</p>
    </main>
    <footer>
      <small>Copyright © Creator Inc.</small>
    </footer>
  </body>
</html>
2. Rendering a template using layout option in front matter

Wrapping extends tag in each of our file isn't super cool,

and we can avoid this by using layout option in front-matter.

Example

Step 1

Consider we have a nunjucks template with 3 blocks.

default.njk

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Page Title</title>
</head>
<body>
  <header>
    {% block header %}{% endblock %}
  </header>
  <main>
    {% block main %}{% endblock %}
  </main>
  <footer>
    {% block footer %}{% endblock %}
  </footer>
</body>
</html>

and a simple html page with content for these 3 blocks

index.html

---
layout: default
---
{% block header %}
<h1>On Laughing</h1>
{% endblock %}
{% block main %}
<p>A laugh draws a lot of painful lines.</p>
{% endblock %}
{% block footer %}
<small>Copyright &copy; Creator Inc.</small>
{% endblock %}

Step 2

Now, Let's run njk.

njk index.html

The current directory will be used for templates.

Result

The result will be something like

dist/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Page Title</title>
  </head>
  <body>
    <header><h1>On Laughing</h1></header>
    <main><p>A laugh draws a lot of painful lines.</p></main>
    <footer><small>Copyright © Creator Inc.</small></footer>
  </body>
</html>
Extra : Configuring layout through data passed

We can go one step further and configure layout it in the data passed with -d or --data

Step 1

Remove front-matter from index.html

index.html

{% block header %}
<h1>On Laughing</h1>
{% endblock %}
{% block main %}
<p>A laugh draws a lot of painful lines.</p>
{% endblock %}
{% block footer %}
<small>Copyright &copy; Creator Inc.</small>
{% endblock %}

Step 2 ( using yml )

data/page.yml

layout: default

We need to run

njk index.html -d data

Note that file name is important here, as we need page.layout property.

Step 2 ( using json )

We can pass a single json file instead of data folder

data.json

{
  "page": {
    "layout": "default"
  }
}

We need to run

njk index.html -d data.json

Result

The result will be same as our previous run (Example 2).

njk's People

Contributors

borracciablu avatar dependabot[bot] avatar greenkeeper[bot] avatar mohitsinghs avatar satoryu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

njk's Issues

Output directory

When using njk src/html/*.html --out dist files are being created in dist/src/html instead of directly to dist. So, for example src/html/index.html gets output to dist/src/html/index.html.

Is it possible to get the output to be dist/index.html instead of including the extra source folders? Thanks for the great CLI tool!

thank you for service

sad to hear njk is no longer receiving lts. i enjoyed it while it lasted. may i ask what alternatives you are using?

Watch mode failed to render

I'm pretty frequently getting messages like the following and an empty file when running in watch mode on Ubuntu 18.10:

ℹ Changed template src/html/index.html
⚠ Failed to render src/html/index.html, null output
✔ Wrote index.html

If I save the file again, it seems to build correctly. I also have the verbose flag enabled, but those are the only messages I'm seeing.

I think it might be related to babel/babel#4332 and postcss/postcss-cli#215, which were both fixed by adding awaitWriteFinish options to chokidar.watch.

globby error

I really wish this package would work like it claims to.

Here's the command line I'm trying to run

njk ./content -t ./templates -o _site/

I have .md files in ./content, .njk templates in ./templates and expecting everything to bundle up nicely in _site/. Instead, globby throws an error.

throw new TypeError('Patterns must be a string or an array of strings');

It looks like your package uses globby.sync twice, the first one seems to have the ['./content'] argument I'm passing but the second time around assigning -t ./templates just sends in undefined. I can see my process.argv is passing in all the arguments that I'm sending. Looks like your commander losing them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.