Giter Club home page Giter Club logo

carrot's Introduction

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.0</version>
</dependency>

With Gradle:

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

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.