Template language
Carrot's template language is actually pretty simple. At the top level, there's three main kinds of tags you can use. They are:
{# comment #}
{{ echo }}
{% tag %}
Comments
{# comment #}
The first type of tag is the simplest, a comment. Comments are stripped from the output and otherwise ignored. Useful also for commenting out bits of other template, since you can just put a # between the { and the % or {.
At this point, I should also just note that you can escape a tag with a backslash, like so:
You can output a literal {% like so: {\%
Same, of course, with the other types of tags.
Echo
{{ <expr> }}
The next type of tag, the "echo" tag is actually just a shortcut for {% echo <expr> %}
. It
simple echos the evaluation of the expression inside the tag.
See the Expressions section for more details about how expression are evaluated.
Tags
{% echo <expr> %}
{% if <expr> %}...{% end %}
{% for identifier in <expr> %}...{% end %}
{% extends <expr> %}...{% end %}
{% include <expr> %}
{% block <expr> %}...{% end %}
Tags are where most of the interesting work happens in your template. See the following pages for details, but
basically there are two types of tags: block tags and inline tags. Inline tags, like the echo
tag don't
tag children and just output directly into the stream. Block tags like if
, for
, etc have
children and can optionally output their children.
You can register your own classes as tags like so:
CarrotEngine engine = new CarrotEngine(
new Configuration.Builder()
.setTagRegistry(
new TagRegistry.Builder().add("tagName", MyTag.class))
.build());
Your tag class must inherit from Tag. More details, and an example, can be found in the custom tags section.