![38% of CISOs Fear They’re Not Moving Fast Enough on AI](https://cdn.sanity.io/images/cgdhsj6q/production/faa0bc28df98f791e11263f8239b34207f84b86f-1024x1024.webp?w=400&fit=max&auto=format)
Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
jinja-to-js
Advanced tools
[![Build Status](https://travis-ci.org/jonbretman/jinja-to-js.svg?branch=master)](https://travis-ci.org/jonbretman/jinja-to-js)
Converts Jinja2 templates into JavaScript functions so that they can be used in the browser.
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.
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.
if
statements (Jinja Docs)for
(Jinja Docs) - see below for supported loop helperswith
(Jinja Docs)include
(Jinja Docs) - see below for exampledefined
- docsundefined
- docscallable
- docsdivisibleby
- docseven
- docsodd
- docsnone
- docsnumber
- docsupper
lower
string
mapping
safe
- docscapitalize
- docsabs
- docsattr
- docsbatch
- docsdefault
- docsfirst
- docsint
- docslast
- docslength
- docslower
- docsslice
- docstitle
- docstrim
- docsupper
- docstruncate
- docsLoop helpers will only work for lists (JS arrays). The following helpers are supported:
loop.index
loop.index0
loop.first
loop.last
loop.length
Includes are supported by taking the following steps:
{% 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 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 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
[![Build Status](https://travis-ci.org/jonbretman/jinja-to-js.svg?branch=master)](https://travis-ci.org/jonbretman/jinja-to-js)
The npm package jinja-to-js receives a total of 0 weekly downloads. As such, jinja-to-js popularity was classified as not popular.
We found that jinja-to-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.