Better template language needed [closed]

爱⌒轻易说出口 提交于 2019-12-03 06:59:58

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 %}

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

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

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

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

html.render( out );

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

Output:

<div>Name: whatever</div>

<div>Friends:

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

</div>
Paul Sweatte

Java+ is a simple preprocessor solution. It just stringifies markup:

System.out.println({{
 <html>
  <body>
   ...
  </body>
</html>}})

It has configurable delimiters and passes through Java code rather than consuming it:

System.out.println({{
<xmlExample>
  <name>{{fpp}}</name>
  <number>{{bar}}</number>
</xmlExample>
}});

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!