Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja-like JS templating language

Tags:

I really like django/jinja2 templating languages. Their syntax is extremely simple and yet is highly versatile. Is there anything similar to that in both syntax and capability in javascript, or if not both, at least in the capability.

I looked at underscore, jquery templates, and mustache templates, and none of them seemed to be what I am looking for.

Additional notes

I think out of all libs (I looked at) mustache is the best but I don't really like the syntax. For example this mustache template

{{#people}}
    {{name}}
{{/people}}
{{^people}}
    No people :(
{{/people}}

compared to django's templates:

{% for person in people %}
    {{ person.name }}
{% empty %}
     No people :(
{% endfor %}`

Also the same thing for applying filters. For example:

{{#filter}}{{value}}{{/filter}}

vs

{{ value|filter }}

I think django/jinja2 approach is more clean and just feels more natural.

So, is there any js library which does templates very similar to django/jinja? If not, I guess I have to live with muschache (or maybe some other good js library - I am open to suggesstions), but it just doesn't feel right.

Thank you.

like image 925
miki725 Avatar asked Jul 12 '12 23:07

miki725


People also ask

Can Jinja be used in JavaScript?

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.

What is the difference between Jinja and Jinja2?

Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.

Is Jinja a template engine?

Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox.


3 Answers

Have a look at Nunjucks, a JS templating engine heavily inspired by Jinja2. It supports block inheritance, macros, filters and much more and works both server (NodeJS) and client-side (most browsers).

like image 132
Jasper Moelker Avatar answered Nov 27 '22 04:11

Jasper Moelker


My JavaScript Jinja implementation can be found here: https://github.com/sstur/jinja

It supports both Jinja and Liquid syntax, runs on the browser and in Node, will compile templates to dependency-free JavaScript, and is about 3K gzipped

http://sstur.com/jinja/demo/

Tests included. Express.js support in progress..

like image 24
sstur Avatar answered Nov 27 '22 06:11

sstur


Link from @pradeek's comment. It is a port of jinja to js.

https://github.com/ericclemmons/jinja.js

like image 33
miki725 Avatar answered Nov 27 '22 06:11

miki725