2.8.0 • Published 5 months ago

minijinja-js v2.8.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 months ago

License Crates.io rustc 1.63.0 Documentation

minijinja-js is an experimental binding of MiniJinja to JavaScript. It has somewhat limited functionality compared to the Rust version. These bindings use wasm-bindgen.

You might want to use MiniJinja instead of Jinja2 when the full feature set of Jinja2 is not required and you want to have the same rendering experience of a data set between Rust, Python and JavaScript.

This exposes a bunch of MiniJinja via wasm to the browser, but not all of it.

This package can be useful if you have MiniJinja templates that you want to evaluate as a sandbox in a browser for a user or on the backend. Given the overheads that this creates size and performance wise, it would not be wise to use this for actual template rendering in the browser.

Example

Render a template from a string:

import { Environment } from "minijinja-js";

const env = new Environment();
env.debug = true;
const result = env.renderStr('Hello {{ name }}!', { name: 'World' });
console.log(result);

Render a template registered to the engine:

import { Environment } from "minijinja-js";

const env = new Environment();
env.addTemplate('index.html', 'Hello {{ name }}!');
const result = env.renderTemplate('index.html', { name: 'World' });
console.log(result);

Evaluate an expression:

import { Environment } from "minijinja-js";

const env = new Environment();
const result = env.evalExpr('1 + 1', {});
console.log(result);

Known Limitations

There are various limitations with the binding today, some of which can be fixed, others probably not so much. You might run into the following:

  • Access of the template engine state from JavaScript is not possible.
  • You cannot register a custom auto escape callback or a finalizer
  • If the engine panics, the WASM runtime corrupts.

Sponsor

If you like the project and find it useful you can become a sponsor.

License and Links

2.8.0

5 months ago