String Interpolation Mode
This is NOT a Play specific feature.
Rythm (starts from v1.0.0-20120630) provides an new feature called SIM, the “String interpolation mode”. With this feature one can use Rythm in a most lightweight way.
String result = Rythm.render("Hello @who!", "Rythm"); // the result is "Hello Rythm!"
The above code is passing render arguments by position, you can also pass arguments by name:
Map<String, Object> args = new HashMap<String, Object>(); args.put("who", "Rythm"); String result = Rythm.render("Hello @who!", args); // the result is "Hello Rythm!"
As a comparison, previously you need a much more verbose way to do the same thing:
String result = Rythm.render("@args String who;Hello @who!", "Rythm");
As you can see with String interpolation mode, you can omit the argument declaration. Might not looks a big deal at first glance, but it literally make calling to Rythm method costs nothing more than a
String.format() call, while you get more than
String.format() in that first you have a faster version (except the first call), and second, you can pass in render arguments by name.
What you don’t have in SIM (String interpolation mode)?
SIM is not aimed to be used in all cases. Rather it is suggest to use SIM only in very simple templates, in short where you can use
String.format(). For comprehensive templates you still need to declare render arguments using
@args keyword. There are the following limitations with SIM:
Complex expression cannot be used in SIM. Since all variables are treated as
Object type, you cannot invoke any specific class fields or methods other than
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
Keywords you cannot use in SIM mode:
- @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
- @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
- @macro, define an macro
- @def, define an inline tag
- @tag, same as
- All extended keywords including the following defined in PlayRythm plugin
How to specify Rythm to enter SIM to render the template?
You don’t specify Rythm to enter SIM, Rythm is intelligent enough to enter SIM automatically if it found there is no keywords specified above in your template.