Carrot

Carrot is a Jinja-like templating library for Java.

License

License

GroupId

GroupId

au.com.codeka
ArtifactId

ArtifactId

carrot
Last Version

Last Version

2.4.5
Release Date

Release Date

Type

Type

jar
Description

Description

Carrot
Carrot is a Jinja-like templating library for Java.
Project URL

Project URL

https://github.com/codeka/carrot
Source Code Management

Source Code Management

https://github.com/codeka/carrot

Download carrot

How to add to project

<!-- https://jarcasting.com/artifacts/au.com.codeka/carrot/ -->
<dependency>
    <groupId>au.com.codeka</groupId>
    <artifactId>carrot</artifactId>
    <version>2.4.5</version>
</dependency>
// https://jarcasting.com/artifacts/au.com.codeka/carrot/
implementation 'au.com.codeka:carrot:2.4.5'
// https://jarcasting.com/artifacts/au.com.codeka/carrot/
implementation ("au.com.codeka:carrot:2.4.5")
'au.com.codeka:carrot:jar:2.4.5'
<dependency org="au.com.codeka" name="carrot" rev="2.4.5">
  <artifact name="carrot" type="jar" />
</dependency>
@Grapes(
@Grab(group='au.com.codeka', module='carrot', version='2.4.5')
)
libraryDependencies += "au.com.codeka" % "carrot" % "2.4.5"
[au.com.codeka/carrot "2.4.5"]

Dependencies

runtime (1)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 jar 3.0.1

Project Modules

There are no modules declared in this project.

Carrot

Build Status Code Coverage

Carrot is a templating library for Java that is similar to the Jinja library from python. Jinja -> Ginger -> Carrot, geddit?

It was originally a fork of http://jangod.googlecode.com/, but that project appears to be abandoned, so I've renamed it, and subsequently (in version 2.x) rewritten it.

Getting Started

With Maven:

<dependency>
  <groupId>au.com.codeka</groupId>
  <artifactId>carrot</artifactId>
  <version>2.4.5</version>
</dependency>

With Gradle:

compile 'au.com.codeka:carrot:2.4.5'

First, you need to create a CarrotEngine, which will hold the environment for parsing templates and processing them:

CarrotEngine engine = new CarrotEngine(new Configuration.Builder()
     .setResourceLocator(new FileResourceLocator.Builder("path/to/templates"))
     .build());

Typically, you'll have a "skeleton" template and a "body" template, where the skeleton defines the overall HTML structure that all your pages share, and the body template is the custom things just for that page. The way you do this is by having your body template extend the skeleton template, like so:

skeleton.html:

<!DOCTYPE html>
<html>
  <head>
    <title>{% block "title" %}{% end %}</title>
  </head>
  <body>
    {% block "content" %}{% end %}
  </body>
</html>

index.html:

{% extends "skeleton.html" %}
{% block "title" %}Hello World{% end %}
{% block "content" %}
  <h1>Hello, World!</h1>
{% end %}

Finally to process a template, you use the process method:

System.out.println(engine.process("index.html", new EmptyBindings()));

Now how do you actually pass data from your application to the template? That's what the bindings are for. Say you have the following in your template:

<p>Hello, {{ name }}!</p>

You'd pass data to that via Bindings, like so:

Map<String, Object> bindings = new TreeMap<>();
bindings.put("name", "Dean");
System.out.println(engine.process("index.html", new MapBindings(bindings)));

Documentation

The documentation is currently pretty sparse, but you can head over to codeka.github.io/carrot to view what there is. Please do not hesitate to open an issue if you have any questions.

Contributing

I love contributions! In general, follow the standard GitHub process for pull requests.

To build locally, you can just do:

gradlew build

Which will put a .jar in build/libs. From there just copy it to your project, test it and so on.

To build the documentation:

gradlew buildDocs

Which will put the docs in build/docs.

Versions

Version
2.4.5
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.0
2.2.2
2.2.1
2.2.0
2.1.0
2.0.0
1.0.1
1.0.0