javascript 类似 Jinja 的 JS 模板语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11462211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:19:00  来源:igfitidea点击:

Jinja-like JS templating language

javascriptjqueryjinja2template-enginelanguage-comparisons

提问by miki725

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.

我真的很喜欢 django/jinja2 模板语言。它们的语法极其简单,但用途广泛。javascript 中的语法和功能是否有类似的东西,或者如果两者都没有,至少在功能方面。

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

我查看了下划线、jquery 模板和 mustache 模板,但它们似乎都不是我想要的。

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

我认为在所有库中(我看过)mustache 是最好的,但我真的不喜欢它的语法。例如这个小胡子模板

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

compared to django's templates:

与 Django 的模板相比:

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

我认为 django/jinja2 方法更干净,感觉更自然。

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.

那么,是否有任何 js 库的模板与 django/jinja 非常相似?如果没有,我想我必须忍受 muschache(或者可能是其他一些好的 js 库 - 我愿意接受建议),但它只是感觉不对。

Thank you.

谢谢你。

采纳答案by miki725

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

来自@pradeek 评论的链接。它是 jinja 到 js 的一个端口。

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

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

回答by Jasper Moelker

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

看看Nunjucks,这是一个受 Jinja2 启发很大的 JS 模板引擎。它支持块继承、宏、过滤器等等,并且可以在服务器(NodeJS)和客户端(大多数浏览器)上工作。

回答by sstur

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

我的 JavaScript Jinja 实现可以在这里找到: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

它支持 Jinja 和 Liquid 语法,在浏览器和 Node 上运行,将模板编译为无依赖的 JavaScript,并且大约 3K gzipped

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

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

Tests included. Express.js support in progress..

包括测试。Express.js 支持正在进行中..

回答by potench

Edit: Not maintained anymore

编辑:不再维护

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

Swig是一个很棒的、类似 Django/Jinja 的模板引擎,用于 node.js 和浏览器。

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

旁注:您可以使用 Djangorequest.is_ajax()有条件地发回未渲染的 Django 模板,并使用 Swig 将其渲染为 JSON 对象。

回答by tamakisquare

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

一个javascript模板库,旨在兼容django的模板语言

Plate

盘子

回答by Kim T

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

twig.js 看起来也很有趣,重量比盘子还轻 https://github.com/justjohn/twig.js

回答by ruralcoder

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

我已经将 Jinja2 与 Python 一起使用,现在我在 Node.js 平台上的另一个项目中使用 jinjs。

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

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