Node.js 有模板引擎吗?

发布于 2024-08-13 02:51:46 字数 78 浏览 7 评论 0原文

我正在尝试使用 Node.js 构建整个 Web 应用程序。是否有类似于(例如)Django 模板引擎之类的模板引擎,至少允许您扩展基本模板?

I'm experimenting with building an entire web application using Node.js. Is there a template engine similar to (for example) the Django template engine or the like that at least allows you to extend base templates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

小鸟爱天空丶 2024-08-20 02:51:47

老实说,Node.js 最好、最简单的模板引擎是(恕我直言)Plates(https://github.com/熨斗/盘子)。您可能还想查看 Node.js 的 Flatiron MVC 框架 (http://flatiron.org)。

Honestly, the best and most simple template engine for Node.js is (IMHO) Plates (https://github.com/flatiron/plates). You might also want to check out the Flatiron MVC framework for Node.js (http://flatiron.org).

情魔剑神 2024-08-20 02:51:47

您可以使用DojoToolkit.org 的dojox.dtl。请注意,dojo 1.7 可以很好地在 NodeJS 上运行并作为服务器端库执行。如果您有兴趣,我可以给您举一个简单的例子。

You can use dojox.dtl of DojoToolkit.org. Note that dojo 1.7 can well run on NodeJS and perform as a server side library. If you're interested, I can give you a simple example.

阿楠 2024-08-20 02:51:46

查看 Node js 模块 wiki 页面。他们列出了所有支持 Node.js 的模板引擎

Check out the Node js modules wiki page. They have listed all the templating engines supporting node.js.

遮云壑 2024-08-20 02:51:46

您应该能够使用 Mustache.js,如果它不起作用,请将问题发送给我,我会修复它,因为无论如何我都将在 Node.js 中使用它们。

http://github.com/janl/mustache.js

我知道它无需 DOM 即可工作因为一堆 CouchDB 独立应用程序在 Spidermonkey 视图服务器中使用它。

You should be able to use mustache.js, if it doesn't work send me the issues and I'll get it fixed because I'm about to be using them in node.js anyway.

http://github.com/janl/mustache.js

I know that it works without a DOM because a bunch of CouchDB standalone apps are using it in a Spidermonkey view server.

转身泪倾城 2024-08-20 02:51:46

如果你喜欢 haml,但想要更好的东西,请查看 http://jade-lang.com for Node,我也写了 haml.js :)

If you like haml, but want something even better check out http://jade-lang.com for node, I wrote haml.js as well :)

止于盛夏 2024-08-20 02:51:46

一直有新的模板引擎出现。

underscore.js为js添加了很多函数式编程支持,并且具有模板化功能。

就在今天,我听说了这个: http://github.com/SamuraiJack/Shotenjin-Joosed

There are new templating engines all the time.

underscore.js adds a lot of functional programming support to js, and has templating.

And just today I heard about this: http://github.com/SamuraiJack/Shotenjin-Joosed

殤城〤 2024-08-20 02:51:46

您应该看一下node-asyncEJS,它被明确设计为采用节点的异步特性.js 考虑在内。它甚至允许模板内的异步代码块。

这是文档的示例:

<html>
  <head>
    <% ctx.hello = "World";  %>
    <title><%= "Hello " + ctx.hello %></title>
  </head>
  <body>

    <h1><%? setTimeout(function () { res.print("Async Header"); res.finish(); }, 2000)  %></h1>
    <p><%? setTimeout(function () { res.print("Body"); res.finish(); }, 1000)  %></p>

  </body>
</html>

You should take a look at node-asyncEJS, which is explicitly designed to take the asynchronous nature of node.js into account. It even allows async code blocks inside of the template.

Here an example form the documentation:

<html>
  <head>
    <% ctx.hello = "World";  %>
    <title><%= "Hello " + ctx.hello %></title>
  </head>
  <body>

    <h1><%? setTimeout(function () { res.print("Async Header"); res.finish(); }, 2000)  %></h1>
    <p><%? setTimeout(function () { res.print("Body"); res.finish(); }, 1000)  %></p>

  </body>
</html>
长发绾君心 2024-08-20 02:51:46

您可以尝试 beardless (它的灵感来自焊接/板):

例如:

{ post:
  { title: "Next generation templating: Start shaving!"
  , text: "TL;DR You should really check out beardless!"
  , comments:
    [ {text: "Hey cool!"}
    , {text: "Really gotta check that out..."}  ]
  }
}

您的模板:

<h1 data-template="post.title"></h1>
<p data-template="post.text"></p>
<div>
  <div data-template="post.comments" class="comment">
    <p data-template="post.comments.text"></p>
  </div>
</div>

输出:

<h1>Next generation templating: Start shaving!</h1>
<p>TL;DR You should really check out beardless!</p>
<div>
  <div class="comment">
    <p>Hey cool!</p>
  </div>
  <div class="comment">
    <p>Really gotta check that out...</p>
  </div>
</div>

You can try beardless (it's inspired by weld/plates):

For example:

{ post:
  { title: "Next generation templating: Start shaving!"
  , text: "TL;DR You should really check out beardless!"
  , comments:
    [ {text: "Hey cool!"}
    , {text: "Really gotta check that out..."}  ]
  }
}

Your template:

<h1 data-template="post.title"></h1>
<p data-template="post.text"></p>
<div>
  <div data-template="post.comments" class="comment">
    <p data-template="post.comments.text"></p>
  </div>
</div>

Output:

<h1>Next generation templating: Start shaving!</h1>
<p>TL;DR You should really check out beardless!</p>
<div>
  <div class="comment">
    <p>Hey cool!</p>
  </div>
  <div class="comment">
    <p>Really gotta check that out...</p>
  </div>
</div>
总攻大人 2024-08-20 02:51:46

我已经为 Simon Willisons djangode 项目 做了一些相当完整的 Django 模板语言移植工作(实用程序Node.js 的函数借用了 Django 中的一些有用的概念)。

请参阅此处的文档。

I have done some work on a pretty complete port of the Django template language for Simon Willisons djangode project (Utilities functions for node.js that borrow some useful concepts from Django).

See the documentation here.

弃爱 2024-08-20 02:51:46

我将 Twig 与 Symfony 一起使用,现在涉足 Node.js,所以我正在查看 https://github .com/justjohn/twig.jshttps://github.com/paularmstrong/swig,如果您使用 django,您可能会喜欢它。

I use Twig with Symfony and am now dabbling in node.js, so I'm looking at https://github.com/justjohn/twig.js and https://github.com/paularmstrong/swig, which you'll probably like if you use django.

︶葆Ⅱㄣ 2024-08-20 02:51:46

如果您正在寻找一种简约的模板方法,可以查看 JSON 模板

功能更齐全的替代方案是 EJS。它与您从 Django 获得的东西有点相似。

对于其中每一个,您的情况可能有所不同 - 它们是为浏览器 Javascript 环境而不是 Node.js 设计的。

If you're looking for a minimalist approach to templates, you can check out JSON Template.

A more full-featured alternative is EJS. It's a bit more similar to something you'd get from Django.

Your mileage may vary for each of these - they're designed for a browser Javascript environment, and not Node.js.

油饼 2024-08-20 02:51:46

警告:JinJs 不再维护。它仍然可以工作,但与最新版本的express不兼容。

您可以尝试使用jinjs。它是 Jinja 的一个端口,Jinja 是一个非常好的 Python 模板系统。您可以像这样使用 npm 安装它:

npm install jinjs

在 template.tpl 中:

I say : "{{ sentence }}"

在 template.js 中:

jinjs = require('jinjs');
jinjs.registerExtension('.tpl');
tpl = require('./template');
str = tpl.render ({sentence : 'Hello, World!'});
console.log(str);

输出将是:

I say : "Hello, World!"

我们正在积极开发它,很快就会有一个好的文档。

WARNING : JinJs is not maintained anymore. It is still working but not compatible with the lastest version of express.

You could try using jinjs. It is a port of the Jinja, a very good Python templating system. You can install it with npm like this :

npm install jinjs

in template.tpl :

I say : "{{ sentence }}"

in your template.js :

jinjs = require('jinjs');
jinjs.registerExtension('.tpl');
tpl = require('./template');
str = tpl.render ({sentence : 'Hello, World!'});
console.log(str);

The output will be :

I say : "Hello, World!"

We are actively developing it, a good documentation should come pretty soon.

╰ゝ天使的微笑 2024-08-20 02:51:46

haml 是 Node.js 的不错选择

http://github.com/creationix/haml-js

haml-js

!!! XML
!!! strict
%html{ xmlns: "http://www.w3.org/1999/xhtml" }
  %head
    %title Sample haml template
  %body
    .profile
      .left.column
        #date= print_date()
        #address= current_user.address
      .right.column
        #email= current_user.email
        #bio= current_user.bio

html

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Sample haml template
</title></head><body><div class="profile"><div class="left column"><div id="date">January 1, 2009
</div><div id="address">Richardson, TX
</div></div><div class="right column"><div id="email">[email protected]
</div><div id="bio">Experienced software professional...
</div></div></div></body></html>

haml is a good choice for node.js

http://github.com/creationix/haml-js

haml-js

!!! XML
!!! strict
%html{ xmlns: "http://www.w3.org/1999/xhtml" }
  %head
    %title Sample haml template
  %body
    .profile
      .left.column
        #date= print_date()
        #address= current_user.address
      .right.column
        #email= current_user.email
        #bio= current_user.bio

html

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Sample haml template
</title></head><body><div class="profile"><div class="left column"><div id="date">January 1, 2009
</div><div id="address">Richardson, TX
</div></div><div class="right column"><div id="email">[email protected]
</div><div id="bio">Experienced software professional...
</div></div></div></body></html>
我不是你的备胎 2024-08-20 02:51:46

我听说过有关 {dust} 的好消息 http://akdubya.github.com/dustjs/#dust

I've heard good things about {dust} http://akdubya.github.com/dustjs/#dust

裸钻 2024-08-20 02:51:46

尝试“vash” - 像 Node.js 的 razor 语法一样的 asp.net mvc

https:// github.com/kirbysayshi/Vash

还可以签出:http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx


// sample
var tmpl = vash.compile('<hr/>@model.a,@model.b<hr/>');
var html = tmpl({"a": "hello", "b": "world"});
res.write(html);

Try "vash" - asp.net mvc like razor syntax for node.js

https://github.com/kirbysayshi/Vash

also checkout: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx


// sample
var tmpl = vash.compile('<hr/>@model.a,@model.b<hr/>');
var html = tmpl({"a": "hello", "b": "world"});
res.write(html);
这个俗人 2024-08-20 02:51:46

Google 的 Closure Templates 是一个原生 JavaScript 模板系统,看起来与 NodeJS 很自然地契合。 以下是一些集成它们的说明

Google's Closure Templates is a natively-JavaScript templating system and a seemingly natural fit with NodeJS. Here are some instructions for integrating them.

羅雙樹 2024-08-20 02:51:46

您尝试过 PURE 吗?
如果您尝试一下,请随时发布任何内容您在论坛中可能遇到的问题

虽然它主要是为浏览器,它与 Jaxer 和 Rhino 配合良好。

我还不了解node.js,但是如果你可以在内存中缓存一些JS和函数,速度应该会更令人印象深刻。

Did you try PURE ?
If you give it a try, feel free to post any trouble you may face at the forum

While it was primarly designed for the browser, it works well with Jaxer and Rhino.

I don't know node.js yet but if you can cache some JS and functions in memory, the speed should be even more impressive.

万水千山粽是情ミ 2024-08-20 02:51:46

Django 模板引擎有一个到 JavaScript 的端口。不过,它已经很长时间没有更新了,但它可能仍然有足够的功能。

http://code.google.com/p/jtl-javascript-template/< /a>

There is a port of the Django templating engine to JavaScript. However, its not been updated for a long time but it may still have enough features.

http://code.google.com/p/jtl-javascript-template/

此岸叶落 2024-08-20 02:51:46

也尝试Yajet。 ;-) 这是我昨天刚刚发布的一个新工具,但我现在已经使用它一段时间了,它稳定且快速(模板被编译为原生 JS 函数)。

尽管代码量很小(缩小后为 8.5K),但它具有 IMO 模板引擎可能的最佳语法和丰富的功能集。它具有允许您引入条件、迭代数组/哈希、定义可重用模板组件等的指令。

Try Yajet too. ;-) It's a new one that I just released yesterday, but I'm using it for a while now and it's stable and fast (templates are compiled to a native JS function).

It has IMO the best syntax possible for a template engine, and a rich feature set despite its small code size (8.5K minified). It has directives that allow you to introduce conditionals, iterate arrays/hashes, define reusable template components etc.

银河中√捞星星 2024-08-20 02:51:46

我从 Twitter 上找到了 hogan.js,并由 Tim O'Reilly 在他的网站上推荐。我没有最佳实践,但我信任 Twitter 和 O'Reilly。你应该尝试...

I found hogan.js from Twitter and recommended by Tim O'Reilly on his site. I have no best practice with it, but I trust on Twitter and O'Reilly. You should try...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文