带有 java 编译器的客户端模板语言(DRY 模板)

发布于 2024-11-26 02:58:27 字数 922 浏览 1 评论 0原文

我希望能够定义一次模板并使用它们从服务器端和客户端渲染 html。 (DRY 原则以及所有这些)

我设想的 API 很简单: render(JSON, template) --> html。

我正在使用 java 框架(实际上是 Play 框架,但我不认为这是特定于框架的)。

我读过很多类似的问题,最新的、最有帮助的是:用于客户端和服务器端渲染的模板语言

我非常同意作者的观点,即像 Mustache 和 Google Closure Templates 这样的明显竞争者不会解决这个问题。 (原因请参阅该帖子)

要求:

  • 必须:客户端渲染
  • 必须:模板文件的客户端缓存
  • 好:模板文件的客户端“编译一次执行多次”以快速 javascript 代码
  • 必须:服务器端渲染
  • NICE:原生 java 实现

我看过很多建议使用 Node.js 进行服务器端模板的帖子。虽然这肯定会起作用(下划线模板,Handlebarsjs,EJS 都可以很好地工作),但我正在努力了解如何将 Node.js 与 java 进行通信/组合/集成,毕竟它仍然是需要输出 JSON 的 java 框架

我见过一些帖子提到了 JVM 和 node.js 之间的一些概念验证通信(通过 http 或使用 JNDI)。然而,目前似乎还没有可用的库,更不用说经过实战检验了。

那么,为了总结起来,您建议哪种客户端模板引擎也可以在 java 中运行(或者通过一些hoops,可以从 jvm 调用)?如果那个“箍”恰好是 Node.js,您建议使用什么通信/库方式?

I want to be able to define templates once and use them to render html from both the server-side as well as the client-side. (DRY principle and all that)

The API that I'm envisioning is simply this: render(JSON, template) --> html.

I'm using a java-framework (actually Play framwork, but I don't think this is framework specific).

I've read a lot of similar questions, the latest, and most helpful being: Templating language for both client-side and server-side rendering.

I pretty much agree with the author that obvious contenders like: Mustache and Google Closure Templates are not going to cut it. (for reasons see that post)

Requirements:

  • MUST: client-side rendering
  • MUST: client-side caching of template-files
  • NICE: client-side 'compile-once execute many times' of template-file to fast javascript-code
  • MUST: server-side rendering
  • NICE: native java implementation

I've seen a bunch of posts suggesting the use of Node.js for server-side templating. Although this would definitely work (underscore templates, Handlebarsjs, EJS would all work just fine) I'm struggeling to see how to communicate/combine/integrate Node.js with java, after all it's still the java framework that needs to output the JSON

I've seen posts mentioning some proof-of-concept communicating between a JVM and node.js (over http or using JNDI) . However, no library, let alone battle-tested, seems to be available at the moment.

So to round things up, what client-side templating engine would you suggest that would run in java as well (or with some hoops, can be called from a jvm) ? And if that 'hoop' happens to be Node.js, what ways of communication/ library would you suggest to use?

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

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

发布评论

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

评论(1

青丝拂面 2024-12-03 02:58:27

我现在选择 Mustache,并期待 Handlebars.js 的 java 实现。
一旦存在,重构路径就不应该那么陡峭。

编辑 - 2012 年 4 月

好的,更新此内容以供将来参考:

100% DRY(甚至客户端 mixins 和 i18N-bundles 都来自同一来源。
此外,Hogan 可以在服务器端预编译模板并打开到客户端的连接,这样客户端在第一次连接时就不必再解析模板了。

快吗?闪电...

I'm going for Mustache for now and anticipating a java implementation for Handlebars.js.
Once that exists, the refactoring-path shouldn't be that steep.

EDIT - april 2012

Ok, updating this for future reference:

  • I'm outsourcing server-side templating to Node.js.
  • communication between java and node.js implemented using sockets. (see: Sending data from node.js to Java using sockets for where I got the idea)
  • Since now I only need a client-lib (or better one that runs in javascript on both client and server-side using node) I can choose more freely. Having become accustomed to Mustache, I've chosen the Hogan parser (by the Twitter guys) ( http://twitter.github.com/hogan.js/ )

100% DRY (even the client-side mixins and i18N-bundles come from the same source.
Moreover, Hogan can precompile the templates server-side and open a connection to the client so the client doesn't have to parse the template anymore on first connect.

Is it fast? Lightning...

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