Question

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.

Was it helpful?

Solution 3

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

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

OTHER TIPS

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

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

Edit: Not maintained anymore

Swig is an awesome, Django/Jinja-like template engine for node.js and the browser.

<h1>{{ pagename|title }}</h1>
<ul>
{% for author in authors %}
    <li{% if loop.first %} class="first"{% endif %}>{{ author }}</li>
{% endfor %}
</ul>

You can add custom tags to achieve various types of template syntax:

{% filter %}{{value}}{% endfilter %}
{% filter 'value' %}
{{ value | filter }}

Side-note: You can use Django's request.is_ajax() to conditionally send back an un-rendered Django template and use Swig to render it with a JSON object.

a javascript template library, aimed at being compatible with django's template language

Plate

twig.js looks really interesting too and weighs less than plate https://github.com/justjohn/twig.js

I have used Jinja2 with Python and now I am using jinjs on a different project on the Node.js platform.

https://github.com/ravelsoft/node-jinjs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top