Echo tag
{% echo <expr> %}
{{ <expr> }}
The echo tag simply outputs the evaluation of the given expression. See the Expressions section for details on how expressions are evaluated.
Note that {{ <expr> }}
is just a shortcut for {% echo <expr> %}
.
Examples
First, let's assume we have the follow code to render the template set up:
CarrotEngine engine = new CarrotEngine();
engine.getConfig().setResourceLocator(...);
Bindings bindings = new MapBindings(ImmutableMap.<String, Object>builder()
.put("num", 5463)
.put("str", "Hello, World")
.put("user", ImmutableMap.of(
"id", 1234,
"name", "Dean"))
.put("details", ImmutableMap.of(
1234, "Some details.")
.build());
System.out.println(engine.process("filename.html", bindings));
Given the following template:
A number: {% echo num %}
A string: {% echo str %}
A user: {% echo user.id %} {% echo user.name %}
User's details: {% echo details[user.id] %}
You'll get the following output:
A number: 5463
A string: Hello, World
A user: 1234 Dean
User's details: Some details.
Things to note:
foo.bar
will return the value of the "bar
" field in objectfoo
. It will first look for a field with exact namebar
. If it can't find one, it'll look for a getter method with name "getBar
". Iffoo
is a map, it will useMap.get("bar")
.foo[bar]
will first evaluate "bar
", then perform exactly the same steps above to get the value infoo
. In particular,foo["bar"]
is actually identical tofoo.bar
.- By default, any HTML in your strings will be automatically escaped. You can disable this globally with Configuration.setAutoEscape. Better still, you can disable it for a single call with html.safe().