Bambang pramusintha

Knowing Hapax

In Hapax, JavaScript on September 17, 2012 at 2:43 am

Hapax is a simple but powerful text templating library for Java.

Hapax is suitable for constructing text output from Java code. The syntax is similar to Google’s ctemplate library, and emphasizes the separation of logic from presentation.

Hapax was designed to be easy to use and have minimal dependencies. Hapax does not depend on any existing web framework, and is suitable for use in servlets, scripting languages (Scala, Groovy, etc), and server-side applications.

Example

package com.xfltr.hapax.examples;
import com.xfltr.hapax.Template;
import com.xfltr.hapax.TemplateDictionary;
import com.xfltr.hapax.TemplateException;
class HelloWorldExample {
  public static void main(String[] args) throws TemplateException {
    Template tmpl = Template.parse("Hello, {{WORLD:h}}");
    TemplateDictionary dict = TemplateDictionary.create();
    dict.put("WORLD", "Iapetus");
    System.out.println(tmpl.renderToString(dict));
  }
}

Features

  • Hapax enforces a strict separation of “view” from application logic.
  • Hapax has zero dependencies on third-party code (except for JUnit).
  • Hapax is well unit-tested.
  • Hapax is based loosely on the syntax and behavior of Google’s ctemplate library.
  • Hapax generate any kind of textual output: config files, HTML, XML, CSS, etc.
  • Hapax templates do not require a compilation step. Templates are parsed at runtime.
  • Hapax has native support for some types of content escaping (JavaScript, XML, HTML, URLs).
  • Hapax does not expect your templates to come from disk: templates can be be generated at runtime, stored in a database, or any other location.

Knowing ctemplate

In C++, ctemplate, Tutorial on September 16, 2012 at 12:35 pm

CTemplate is a simple but powerful template language for C++. It emphasizes separating logic from presentation: it is impossible to embed application logic in this template language.

Here’s an example of how to use it: not complete, but gives a good feel for what the ctemplate API looks like.

Here is a simple template file:

Hello {{NAME}},
You have just won ${{VALUE}}!
{{#IN_CA}}Well, ${{TAXED_VALUE}}, after taxes.{{/IN_CA}}

Here is a C++ program to fill in the template, which i assume is stored in the file ‘example.tpl':

#include <cstdlib>
#include <iostream>  
#include <string>
#include <ctemplate/template.h>  

int main() {
  ctemplate::TemplateDictionary dict("example");
  int winnings = rand() % 100000;
  dict["NAME"] = "John Smith";
  dict["VALUE"] = winnings;
  dict.SetFormattedValue("TAXED_VALUE", "%.2f", winnings * 0.83);
  // For now, assume everyone lives in CA.
  // (Try running the program with a 0 here instead!)
  if (1) {
    dict.ShowSection("IN_CA");
  }
  std::string output;
  ctemplate::ExpandTemplate("example.tpl", ctemplate::DO_NOT_STRIP, &dict, &output);
  std::cout << output;
  return 0;
}

If you are interested in this templating language but are programming in Java, consider Hapax, which is similar to ctemplate.

Knowing JSON Template

In JavaScript, JSON, Python, Themplate on September 16, 2012 at 12:26 pm

JSON Template is a minimal but powerful templating language, currently implemented in both Python and JavaScript.

To use it, simply copy the Python file or the JavaScript file into your project. Neither has any external dependencies.

Since there are over one bajillion template languages for Python alone, it deserves some explanation. But first here are some examples.

Simple example

>>> import jsontemplate
>>> jsontemplate.expand('Hello {name}', {'name': 'world'})
'Hello world'

This Python example should look familiar to many people.* The JavaScript version is a straightforward translation of this API.

Full Example

To read this example, keep in mind the following:

  • A variable can be substituted by surrounding its name with curly braces: {}
  • Template “directives” start with a period: .
  • In place of for loops and if statements, the basic idea is that the template declares sections and repeated sections. The sections are named after keys in the JSON data dictionary. The presence or absence of the keys in the JSON dictionary implicitly determines if and how many times a section is expanded.
  • If a JSON dictionary key is missing or empty ([], {}, or null), then the {.or} section is expanded.

These few constructs are surprisingly powerful.

The following example uses most of the language, but not all.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: