1. Jairo Luiz
  2. haml4j

Overview

HTTPS SSH

Description

Haml4J is a Java wrapper that interprets a Haml templates.

Installation

For Maven users:

Add this respository to pom.xml:

<repositories>
  ...
  <repository>
    <id>tangzero-repo</id>
    <url>http://github.com/tangzero/repository/raw/master/</url>
  </repository>
  ...
</repositories>

Then, add Haml4J dependency to pom.xml:

<dependencies>
  ...
  <dependency>
    <groupId>com.github.tangzero</groupId>
    <artifactId>haml4j</artifactId>
    <version>1.0</version>
  </dependency>
  ...
</dependencies>

Usage

Standalone

First, create a Haml template like this:

!!!
%html
  %head
    %title= @title
  %body
    %h2= @message

Instantiate a template:

Template template = new Template(new File("template.haml"));

And create a context:

Map<String, Object> context = new HashMap<String, Object>();
context.put("title", "Ham4J");
context.put("message", "Simple message");

Now, evaluate:

String output = template.render(context);

Output is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Haml4J</title>
  </head>
  <body>
    <h2>Simple Message</h2>
  </body>
</html>

With a web project

Put in your web.xml:

<servlet>
  <servlet-name>haml4j</servlet-name>
  <servlet-class>com.github.tangzero.haml4j.Haml4JServlet</servlet-class>
  <init-param>
    <param-name>TemplatePath</param-name>
    <param-value>/haml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>haml4j</servlet-name>
  <url-pattern>*.haml</url-pattern>
</servlet-mapping>