Jinja-like JS templating language

miki725 picture miki725 · Jul 13, 2012 · Viewed 11.3k times · Source

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.

Answer

Jasper Moelker picture Jasper Moelker · Apr 3, 2014

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).