需要更好的模板语言 [英] Better template language needed

查看:108
本文介绍了需要更好的模板语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道这个 询问 之前。但大多数问题都是在两年前,今天必须有一个更好的答案。

Yes, I know that this has been asked before. But most of the questions were two years ago, and there's got to be a better answer today.

我需要一种模板语言来呈现HTML。要求:

I need a template language for rendering HTML. Requirements:


  1. 必须可以从Java调用。

  1. Must be callable from Java.

不能是Freemarker,Velocity或StringTemplate。我们已经使用Freemarker一年了,而且速度太慢了。它在负载下消耗了50%的CPU周期。 Velocity比Freemarker更糟糕,StringTemplate也有自己的问题。

Must not be Freemarker, Velocity, or StringTemplate. We've been using Freemarker for a year, and it's too slow. It consumes 50% of our CPU cycles under load. Velocity is worse than Freemarker, and StringTemplate has got its own issues.

它必须使用JSON或其Java等价物,即地图,列表和基元。

It must consume JSON, or its Java equivalent, i.e. Maps, Lists, and primitives.

我开始让Node.js羡慕。在过去的一年里,Javascript已经有了大量的模板活动,但对Java来说却很少(我知道)。

I'm starting to get Node.js envy. There has been a ton of template activity for Javascript in the last year, but little for Java (that I know of).

理想的语言看起来像 Liquid ,仅适用于Rails。

The ideal language would look like Liquid, which exists only for Rails.

Jade 存在于Java中,但我不希望它附带的所有HTML重新格式化。

Jade exists for Java, but I don't want all the HTML reformatting that comes with it.

更新

我最终确定 Handlebars ,也是可用于Java 。两者都很好。

I eventually settled on Handlebars, which is also available for Java. Both work great.

推荐答案

Chunk 对json友好。 JSON可以用作控制器代码中的标记值,也可以用作exec / macro调用的模板本身。

Chunk is json-friendly. JSON can be used as a tag value in your controller code or in the templates themselves for exec/macro calls.

{% exec %}
  {% data @json %}
    { name: "whatever",
      vitals: ["an","array","of","data"],
      friends: [{name: "bob"},{name: "crystal"}]
    }
  {% enddata %}

  <div>Name: {$name}</div>

  {% if ($friends) %}
    <div>Friends:

      <ul>
      {% loop in $friends as $friend %}
        <li>{$friend.name}</li>
      {% endloop %}
      </ul>

    </div>
  {% endif %}

{% endexec %}

或者,只需使用内部模板并从java端注入json。

Or, just use the inner template and inject the json from the java side.

src / themes / example.chtml

src/themes/example.chtml

  <div>Name: {$name}</div>

  {% if ($friends) %}
  <div>Friends:

    <ul>
    {% loop in $friends as $friend %}
     <li>{$friend.name}</li>
    {% endloop %}
    </ul>

  </div>
  {% endif %}

MyController.java

MyController.java

Theme theme = new Theme();
Chunk html = theme.makeChunk("example");

html.set("name", "whatever");
html.set("vitals", getJsonArray() );
html.set("friends", getJsonFriendObjects() );

html.render( out );

只要getJsonXXX()方法返回实现List和Map的内容,Chunk就会将其粘贴到模板正确(即使这些列表和地图嵌套更多列表和地图)。

As long as the getJsonXXX() methods return something that implements List and Map, Chunk will glue it into the template correctly (even if those Lists and Maps nest more Lists and Maps).

输出:

<div>Name: whatever</div>

<div>Friends:

  <ul>
   <li>bob</li>
   <li>crystal</li>
  </ul>

</div>

这篇关于需要更好的模板语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆