New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

jinja-to-js

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jinja-to-js

[![Build Status](https://travis-ci.org/jonbretman/jinja-to-js.svg?branch=master)](https://travis-ci.org/jonbretman/jinja-to-js)

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
increased by60%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

Jinja to JS

Converts Jinja2 templates into JavaScript functions so that they can be used in the browser.

What is it?

Jinja2 is a very fast templating language and therefore is ideal for use with Python web framework like Django, however there are many use cases for wanting to share the same template between the server and the browser. Instead of writing a Jinja implementation in JavaScript (there are already a few of those) jinja-to-js uses the Python Jinja2 library to parse a template into an AST (http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment.parse) and uses that AST to output a JavasScript function/module.

Example

First install jinja-to-js using pip:

$ pip install jinja-to-js

Lets assume we have a Jinja template called names.jinja.

{% for name in names %}
    {{ name }}
{% endfor %}

We can turn this into a JavaScript module like so:

$ jinja_to_js -f ./names.jinja -o ./names.js -m es6

names.js will now contain:

export default function template(context) {
    /* JS code here */
};

The -m option specified the module type, which can be amd, commonjs, es6 or not provided at all which will result in jinja-to-js just outputting a named JS function.

Supported Features

Supported tests

Supported filters

Loop Helpers

(Jinja Docs)

Loop helpers will only work for lists (JS arrays). The following helpers are supported:

  • loop.index
  • loop.index0
  • loop.first
  • loop.last
  • loop.length
Includes (Jinja Docs)

Includes are supported by taking the following steps:

  • If a template contain an include, for example {% include 'bar.jinja' %}, then you must provide a function called include on the context object passed to the template. This function will be called with the name of the template to be included and should return the compiled version of this template.
  • The returned template function is called with the current context as it's only argument.
  • The return value of this function is output into the current template.

The following shows a simple example of this in practice.

var templates = {
    'bar': theCompiledFnForBar,
    'foo': theCompiledFnForFoo
};

function getTemplate(name) {
    return templates[name.replace(/\.jinja$/, '')];
}

function render(name, context) {
    context.include = getTemplate;
    return getTemplate(name)(context);
}

render('foo');
Template Inheritance (Jinja Docs)

Template inheritance is supported, including the {{ super() }} function. The name of the template to be extended from must be a string literal as it needs to be loaded at compile time.

Parent

{% block content %}
    The default content.
{% endblock

Child

{% block content %}
    {{ super() }}
    Additional content.
{% endblock %}

FAQs

Package last updated on 28 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc