String.format()
.
String result = Rythm.render("Hello @who!", "Rythm");As a comparison, previously you need the following code:
String result = Rythm.render("@args String who;Hello @who!", "Rythm");It's really annoy to declare render argument types for such a simple template because we could treat the argument used in the expression as an Object. Yes, this is exactly what SIM did for you, automatically declare the argument reference to
java.lang.Object
type, and save your typing. Looks not a big deal, but it literally make Rythm.render()
an replacement of String.format()
for most cases. You get two benefit from Rythm SIM over String format:
- performance:
Rythm.render
is 2 times faster thanString.format
except the first call - You can pass in arguments not only by position, but also by name
Map<String, Object> args = new HashMap<String, Object>(); args.put("who", "world"); String result = Rythm.render("Hello @who!", args);
SIM Limitation
So as mentioned above SIM only applies to simple templates. The question is what defines a simple template?-
First you can only have single variables in an expression, reference to class fields or method or any combination of two or more variables is not allowed. If you want to use these advanced expression, you must declare the type of the render arguments explicitly
Rythm.render("the name is @user.username", user); // bad Rythm.render("the name is @name", user.username); // good Rythm.render("@args User user;the name is @user.username", user); // good Rythm.render("the sum is @(left + right)", left, right); // bad Rythm.render("the sum is @sum", left + right); // good Rythm.render("@args int left, int right;the sum is @(left + right)", left, right); // good
-
You cannot use some keywords. In other words, some Rythm template features are not available to SIM. Here is a list of Rythm keywords you should avoid to use in SIM:
- @args, declare template argument variables
- @extends, extends a layout template
- @section, define a template part to be inserted in the layout template
- @render, used in layout template to render a template section defined in sub template
- @doLayout, same as
@render
- @doBody, call back tag body
- @include, include another template content in place
- @set, set template variable to be fetched by layout template
- @get, get the template variable set in the sub template
- @expand, execute/expand an macro
- @exec, same as
expand
- @macro, define an macro
- @def, define an inline tag
- @tag, same as
@def
- All extended keywords including the following defined in PlayRythm plugin
How to render with SIM
Now the question is how to specify Rythm to render with SIM instead of ordinary rendering mode. The answer is simple, if your template does not contains the keywords listed above, Rythm will render the template in SIM.Foot notes: Rythm Template Engine is a static typed Java template engine using Razor like syntax. There is a full feature set demo hosted on GAE. The source code is hosted on github