§ Quick Take

import { strict as assert } from "assert";
import isJinjaSpecific from "regex-jinja-specific";

assert.equal(
  isJinjaSpecific().test(
    `<div>{{ '%.2f'|format(3.1415926) }}</div>`
  ),
  true
);

// in case of ambiguous, Nunjucks-or-Jinja code
assert.equal(
  isJinjaSpecific().test(`<div>{{ value }}</div>`),
  false
);

§ Purpose

Nunjucks and Jinja templating languages' syntax is mostly the same. There are certain code patterns which are Jinja-specific, mostly coming from Python functionality. This program, a regex, matches those patterns.

  • Namespaces

    {% set x = namespace(blablabla) %}

  • Backwards pattern

    {{'oodles' if crambles else 'brambles'}}

  • Python digit formatting

    '%.2f'|format(...

Often Python-specific features can be patched to make code render in Nunjucks.

This is not a definite detection tool but rather a minimal, fun attempt to programmatically distinguish between the two languages.

§ API

isJinjaSpecific()

In other words, it's a function which returns a RegExp object.

§ Changelog

See it in the monorepo opens in a new tab, on Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 detect-templating-language 1.1.1
Detects various templating languages present in string
📦 stristri 2.0.0
Extracts or deletes HTML, CSS, text and/or templating tags from string
📦 regex-empty-conditional-comments 1.9.1
Regular expression for matching HTML empty conditional comments
📦 regex-is-jsp 1.1.1
Regular expression for detecting JSP (Java Server Pages) code
📦 regex-is-jinja-nunjucks 1.1.1
Regular expression for detecting Jinja or Nunjucks code