GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing jknack.github.io — {{ Handlebars.java }} - TypeSafe templates in Handlebars.java TypeSafe templates in Handlebars.javaIn this section you will learn how to create and use type safe templates in Handlebars.java. TypeSafe templates are created by extending the public interface TypeSafeTemplate<T> { void apply(T context, Writer writer) throws IOException; String apply(T context) throws IOException; }
The Creating a type safe template
Let's see how to create a
UserTemplate:
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 a
Failing if
ageis 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?
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. |