• Stars
    star
    167
  • Rank 226,635 (Top 5 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created almost 12 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

JavaScript templating engine based on Jinja2

Deprecation Notice

This project has not been actively maintained for almost 7 years at the time I'm writing this. I do not recommend using this. There are plenty of better, more modern choices out there these days.

JavaScript templating engine based on Jinja2

Jinja is one of the most used template engines for Python. This project is a JavaScript implementation with emphasis on simplicity and performance, compiling templates into readable JavaScript that minifies well. It is designed to run on Node or in the browser and weighs in at 8.3KB minified / 3.2K gzip'd.

Jinja is inspired by Django's templating system, just like Ruby's Liquid and PHP's Twig. They all have similar syntax for rendering variables, looping and filtering. This implementation aims to be compatible with Jinja2 and Liquid, but Twig is mostly the same also.

Like its homepage says, "Jinja is beautiful":

{% extends "layout.html" %}
{% block body %}
  <ul>
  {% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
  {% endfor %}
  </ul>
{% endblock %}

Demo

View Online Demo here.

Features

This implementation of Jinja supports auto-escaping output by default, extensible filters, template inheritance, block scope, for/else and safe compilation to dependence-free javascript function.

We also add support for dynamic getter methods. For instance, lets say a given object products has a method _get() and a non-existent property is referenced like {{ products.related }}. Jinja will call products._get('related') and attach the result to products.related. Then it will continue processing the template.

It should run on any browser or JS environment that supports ES5 (use es5-shim on IE8 and below) and JSON. No other libraries required.

Documentation

Detailed documentation can be found here.

Compatibility with Liquid

Liquid markup by Shopify is a Ruby template language very similar to Jinja (it is based on Django just like Jinja) but it has a few differences. For compatibility, we have implemented the Liquid syntax as well as the Jinja syntax in the following cases:

  • Liquid: {{{ html }}} is equivalent to Jinja {{ html | safe }}
  • Liquid: {{ string | split: ',' }} is equivalent to Jinja: {{ string | split(',') }}
  • Liquid: {{ assign a = 2 }} is equivalent to Jinja: {{ set a = 2 }}

In those cases, this implementation will allow either syntax. However the following is not implemented:

  • Liquid: {% comment %} my comment {% endcomment %}

You must use Jinja's comment syntax: {# my comment #}

Differences from Python's Jinja2

This implementation deviates from the official Jinja2 as follows:

  • Line statements, cycle, super, macro tags and block nesting are not implemented
  • Auto escapes html by default (the filter is "html" not "e")
  • Only "html" and "safe" filters are built in
  • Filters are not valid in expressions; foo|length > 1 is not valid
  • Expression Tests (if num is odd) not implemented (is translates to == and isnot to !=)

This is mostly to keep the codebase and rendering logic simple. The goal of this implementation is to be slim and compile to readable JavaScript. There are other projects that aim to implement the complete Jinja2 spec.

Also note:

  • if property is not found, but method '_get' exists, it will be called with the property name (and cached)
  • {% for n in obj %} iterates the object's keys; get the value with {% for n in obj %}{{ obj[n] }}{% endfor %}
  • subscript notation a[0] takes literals or simple variables but not a[item.key]
  • .2 is not a valid number literal; use 0.2

To-Do:

  • ExpressJS Support
  • variables as functions: getUser(4).name
  • properties as methods: users[0].getName()

Test Coverage

The tests use Mocha and were adapted from a similar and excellent project.

Existing Implementations

There are several existing JavaScript implementations of the Jinja family of templating languages, many of which have different objectives than this project and may be suitable for some projects but not others. Some worth mentioning include Nunjucks, Swig, JinJS, Plate and Liquid.js.

More Repositories

1

react-rte

Pure React rich text WYSIWYG editor based on draft-js.
JavaScript
2,864
star
2

draft-js-utils

DraftJS: import/export ContentState to and from HTML/Markdown
JavaScript
883
star
3

js2php

JavaScript to PHP source transformation
PHP
151
star
4

node-websocket-tunnel

Tunnel TCP connections over Secure WebSocket
JavaScript
100
star
5

draft-js-import-html

62
star
6

ck-dragresize

CKEditor dragable image resizing
JavaScript
59
star
7

nbit

A zero-dependency, strongly-typed web framework for Bun, Node and Cloudflare workers
TypeScript
58
star
8

draft-js-import-markdown

57
star
9

draft-js-export-markdown

DraftJS: Export ContentState to Markdown
55
star
10

draft-js-import-element

28
star
11

shopify-ftp

FTP Proxy for Shopify
JavaScript
25
star
12

draft-js-utils-deprecated

Useful utilities to speed up development with draft.js
19
star
13

ckeditor-ui

CKEditor Skin Design
JavaScript
11
star
14

xcurl

A curl clone in Node
TypeScript
10
star
15

node-jetbrains

Command Line tools for JetBrains projects (IntelliJ/WebStorm/etc)
JavaScript
4
star
16

node-frontend

Nginx-style server in Node
JavaScript
3
star
17

hux-api

This is a Prisma-based REST API for Hux, an app for photo sharing.
TypeScript
3
star
18

assembler

Assemble and minify your assets for deployment
JavaScript
2
star
19

hux-rn

Hux, an app for photo sharing. Built with React Native.
TypeScript
1
star
20

synthetic-dom

1
star
21

supports

Basic JS functions to determine environment support
JavaScript
1
star
22

hello-rn

TypeScript
1
star
23

lean-package

A tool for cleaning package.json before publishing
TypeScript
1
star
24

hux-flutter

Hux, an app for photo sharing. Built with Flutter.
Dart
1
star
25

pres-fibers

Presentation for BrisJS 2012/06/04
JavaScript
1
star
26

rn-demo

TypeScript
1
star