My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

1. Overview

Hyper-Text Template Language and Engine (HTTL) based on HTML for JAVA.

  • Fast
    • Strongly typed and compiled to bytecode.
  • Simple
    • Minimum instruction set, Keep it simple and stupid.
  • Intuitive
    • The syntax to meet the HTML and JAVA developer's intuition.
  • Friendly
    • Self-description template for IDE content assist.

2. Performance

Engine Version Initialize Compile First Total TPS Template Case
Freemarker 2.3.18 79ms 125ms 78ms 16,934ms 590t/s books.ftl FreemarkerCase.java
Velocity 1.7 31ms 110ms 31ms 19,278ms 518t/s books.vm VelocityCase.java
Smarty4j 1.0.0-jdk5 0ms 78ms 0ms 21,653ms 461t/s books.st Smarty4jCase.java
Httl 0.1.0 78ms 547ms 0ms 2,077ms 4,814t/s books.httl HttlCase.java
Java 1.6.0_18 0ms 0ms 0ms 2,016ms 4,960t/s Books.java JavaCase.java

3. Example

3.1 Template Example

books.httl:

<html define="User user, Book[] books">
  <body>
    <table if="user.role == 'admin'">
      <tr foreach="book in books">
        <td>${book.title}</td>
      </tr>
    </table>
  </body>
</html>

3.2 Config Example

httl.properties:

#extensions
cache=com.googlecode.httl.support.caches.StrongCache
loader=com.googlecode.httl.support.loaders.ClasspathLoader
parser=com.googlecode.httl.support.parsers.AttributeParser
resolver=com.googlecode.httl.support.resolvers.DfaResolver
compiler=com.googlecode.httl.support.compilers.JdkCompiler
replacer=com.googlecode.httl.support.replacers.ClearBlankReplacer
formatter=com.googlecode.httl.support.formatters.DateFormatter
filter=com.googlecode.httl.support.filters.EscapeHtmlFilter
functions=com.googlecode.httl.support.functions.DefaultFunction

#properties
import.packages=java.util,com.xxx
template.directory=
attribute.namespace=
cache.capacity=0
compile.directory=
java.version=1.6
input.encoding=UTF-8
output.encoding=UTF-8
locale=zh_CN
time.zone=+8
date.format=yyyy-MM-dd HH:mm:ss
number.format=###,##0.###
null.value=
true.value=true
false.value=false
sequences=Mon Tue Wed Thu Fri Sat Sun Mon,Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan

3.3 API Example

Servlet.java:

import com.googlecode.httl.*;

// Get engine (Singleton instance, multi-thread shared.)
Engine engine = Engine.getEngine();

// Create context (Prototype instance, thread-local hold.)
Map<String, Object> context = new HashMap<String, Object>();
context.put("user", userService.findUser(userId));
context.put("books", bookService.findUserBooks(userId));

// Render template to response.
engine.getTemplate("books.httl").render(context, response.getOutputStream());

4. Download

  • Maven:
  • <project>
        <dependencies>
            <dependency>
                <groupId>com.googlecode.httl</groupId>
                <artifactId>httl</artifactId>
                <version>0.2.0</version>
                </dependency>
        </dependencies>
        <repositories>
            <repository>
                <id>httl-repository</id>
                <name>Httl Repository</name>
                <url>http://httl.googlecode.com/svn/maven/</url>
            </repository>
        </repositories>
    <project>

5. Syntax

5.1 Template Syntax

Based on HTML comment:

5.1.1 Interpolation Statement

  • ${expression}
    • Outputs the expression result to the page.
    • e.g. ${user.name}
  • $!{expression}
    • Outputs the expression result to the page without filter.
    • e.g. $!{body}

5.1.2 Definition Statement

  • <html define="type name , type name">
    • Defines the input variable type.
    • e.g. <html define="User user, Book[] books">
  • <td set="name = expression">
    • Establishes the value of a reference.
    • e.g. <td set="price = book.price * book.discount">

5.1.3 Condition Statement

  • <tr if="expression">
    • Output conditional on truth of statements.
    • e.g. <tr if="user.role == 'admin'">...</tr>
  • <tr elseif="expression">
    • Output conditional on truth of statements.
    • e.g. <tr if="user.role == 'admin'">...</tr><tr elseif="user.role == 'member'">...</tr>
  • <tr else="">
    • Output conditional on truth of statements.
    • e.g. <tr if="user.role == 'admin'"-->...</tr><tr else="">...</tr>

5.1.4 Iteration Statement

  • <tr foreach="name in expression">
    • Loops through a list of objects.
    • Status: ${foreach.index} ${foreach.size} ${foreach.first} ${foreach.last}
    • e.g. <tr foreach="book in books>...</tr>
  • <tr breakif(expression)>
    • Breaks the loop when the conditions match.
    • e.g. <tr foreach="book in books" breakif="foreach.index == 10">...</tr>

5.1.5 Block Statement

  • <table block="name">
    • Assigns a block of template to a reference.
    • e.g. <table block="xxx">...</table> $!{xxx}
  • <table macro=name(type name, type name)">
    • A repeated segment of a template, as required.
    • e.g. <table macro="xxx(Book[] books)">...</table> $!{xxx(books)}

5.1.6 Literal Statement

  • <!--## content -->
    • Hidden the comment content.
    • e.g. <!--## This is menu -->
  • <![CDATA[## content ]]>
    • No parse the literal content.
    • e.g. <![CDATA[## This is menu ]]>
  • \#, \$, \\
    • Escape the special symbol.
    • e.g. <!--\#xxx --> \${xxx}

5.2 Expression Syntax

Based on JAVA expression:

5.2.1 Operator Expression

5.2.1.1 Collection Operator

  • Sequence: 1..3
  • List: [123, 'abc']
  • Map: ["xxx": 123, "yyy": "abc"]

5.2.1.2 Date Operator

  • date1 > date2
  • date1 >= date2
  • date1 < date2
  • date1 <= date2

5.2.1.3 Logical Operator

  • if="object"
    • if="object != null"
  • if="string"
    • if="string!= null && string.length() > 0"
  • if="list"
    • if="list != null && list1.size() > 0"
  • foreach="item in list1 | list2"
    • foreach="item in list1 != null && list1.size() > 0 ? list1 : list2"

5.2.2 Function Expression

5.2.2.1 System Function

  • now()
  • random()
  • uuid()

5.2.3.2 File Function

  • include("template.httl")
    • include("template.httl", "UTF-8")
    • include(locale("i18n-template.httl"))
  • read("text.txt")
    • read("text.txt", "UTF-8")
    • read(locale("i18n-text.txt"))

5.2.2.3 Collection Function

  • sort(list)
    • for="item : sort(list)"
  • cycle(item, item)
    • set="Cycle colors = cycle('red', 'blue', 'green')" ${colors.next}
Powered by Google Project Hosting