GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Fork me on GitHub
  • ZIP
  • TAR
  • Maven

This view

  • Original page
  • New address

GeezerVison is showing jknack.github.io — {{ Handlebars.java }} - TypeSafe templates in Handlebars.java

Handlebars.java

Logic-less and semantic templates with Java


by Edgar Espina Theme by mattgraham

TypeSafe templates in Handlebars.java

In this section you will learn how to create and use type safe templates in Handlebars.java.

TypeSafe templates are created by extending theTypeSafeTemplateinterface

public interface TypeSafeTemplate<T> {

  void apply(T context, Writer writer) throws IOException;

  String apply(T context) throws IOException;
}

TheTypeSafeTemplateinterface give you a generic version of theapplymethods, but this isn't all.

Creating a type safe template

Let's see how to create aUserTemplate:
public interface UserTemplate extends TypeSafeTemplate<User> {

  public UserTemplate setAge(int age);

  public UserTemplate setRole(String role);
}
Instantiating a user template:
UserTemplate userTmpl = hbs.compileInline("{{name}} is {{age}} years old!")
  .as(UserTemplate.class);

userTmpl.setAge(32);

userTmpl.apply(new User("Edgar"));

Checking if a variable is present

Beside we have asetAgemethod how can we sure that value has been provided? One alternative, is to provide aMissingValueResolverto fail ifageisn't present.

Failing ifageis missing:
MissingValueResolver missingValueResolver = new MissingValueResolver() {
    public String resolve(Object context, String name) {
      throw new IllegalStateException("Missing variable: " + name);
    }
  };
Handlebars hbs = new Handlebars().with(missingValueResolver);

UserTemplate userTmpl = hbs.compileInline("{{name}} is {{age}} years old!")
  .as(UserTemplate.class);

userTmpl.setAge(32);

userTmpl.apply(new User("Edgar"));

Want to contribute?

  • Fork the project on Github.
  • Create an issue or fix one from the issues list.
  • Share your ideas or ask questions on mailing list - don't hesitate to write a reply - that helps us improve javadocs/FAQ.
  • If you miss a particular feature - browse or ask on the mailing list - don't hesitate to write a reply, show us a sample code and describe the problem.
  • Write a blog post about how you use or extend Handlebars.java.
  • Please suggest changes to javadoc/exception messages when you find something unclear.
  • If you have problems with documentation, find it non intuitive or hard to follow - let us know about it, we'll try to make it better according to your suggestions. Any constructive critique is greatly appreciated. Don't forget that this is an open source project developed and documented in spare time.

Thank you, for reading the Handlebars.java blog.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.