Custom tags

Custom tags are where the real power of Carrot comes from. You can register your own classes to be called when a custom tag is encountered in the template. Custom tags are extremely powerful, and there's nothing that any of the built-in tags can do that a custom tag could not also do.

To create a custom tag, you create a class that inherits from Tag, and register it in the tag registry, like so:

  CarrotEngine engine = new CarrotEngine(
      new Configuration.Builder()
          .setTagRegistry(
              new TagRegistry.Builder().add("mytag", MyTag.class))
      .build());

Then, whenever the engine encounters a {% mytag %} it will call your tag's code to render the result.

The best way to understand how to write a custom would be to look at the source for one of the built-in tags, which you can find here and the documentation for which you can find here.