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 }} - The Mustache Context Stack in Handlebars.java The {{ Mustache }} Context Stack in Handlebars.javaIn this section you will learn how to interact with the Context Stack in Handlebars.java. The Context StackMustache is a contextual and stack based template engine. The context stack is where the your data live.
The Context class represent the context stack in Handlebars.java.
You rarely need to instantiate a String output = template.apply(model);
// or
template.apply(model, writer);
The Resolving values
The resolution algorithm looks something like:
while (context != null && context.get("value") == null) { context = context.parent; } A value will be resolved in the current context. If the value isn't here, the parent context need to resolved the value. If the value isn't in the whole stack, the value is resolved to null and nothing will be rendered. Extending the context stackA common use case in web applications is to access to the logged user from a web page.
A first try might looks like:
User user = ...; Object model = ...; Map modelWithUser = new HashMap(); modelWithUser.put("user", user); modelWithUser.put("model", model); template.apply(modelWithUser); This isn't so bad but there are at least two drawbacks:
A better approach is to use the
combinemethod:
User user = ...;
Object model = ...;
Context context = Context
.newBuilder(model)
.combine("user", user)
.build();
template.apply(context);
We definitely fix the second drawback described before.
The first one depends on your application architecture. For example,
if you have a Value resolvers
A The JavaBeanValueResolverLet you access to JavaBean properties. It is registered by default. Context context = Context
.newBuilder(model)
.resolver(JavaBeanValueResolver.INSTANCE)
.build();
template.apply(context);
The MapValueResolver
Let you access to Context context = Context
.newBuilder(model)
.resolver(MapValueResolver.INSTANCE)
.build();
template.apply(context);
The FieldValueResolverLet you access to instance fields (private or public). Context context = Context
.newBuilder(model)
.resolver(FieldValueResolver.INSTANCE)
.build();
template.apply(context);
The MethodValueResolverLet you access to public methods. Context context = Context
.newBuilder(model)
.resolver(MethodValueResolver.INSTANCE)
.build();
template.apply(context);
Using multiples value resolversYou can combine any number of value resolvers according to your application data.
Access to JavaBean and Map properties:
Context context = Context
.newBuilder(model)
.resolver(
MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE
)
.build();
template.apply(context);
Access to JavaBean, Map properties and public methods:
Context context = Context
.newBuilder(model)
.resolver(
MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE,
MethodValueResolver.INSTANCE
)
.build();
template.apply(context);
Access to JavaBean, Map properties and instance fields:
Context context = Context
.newBuilder(model)
.resolver(
MapValueResolver.INSTANCE,
JavaBeanValueResolver.INSTANCE,
FieldValueResolver.INSTANCE
)
.build();
template.apply(context);
ConclusionThe
Where a For example,
if your service layer returns the widely used Want to contribute?
Thank you, for reading the Handlebars.java blog. |
| You are visitor 01402520 — 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. |