Package com.github.jknack.handlebars
Class Options
java.lang.Object
com.github.jknack.handlebars.Options
Options available for
Helper.apply(Object, Options).
Usage:
Options options = new Options.Builder(handlebars, context, fn)
.build();
Optionally you can set parameters and hash table:
Options options = new Options.Builder(handlebars, context, fn)
.setParams(new Object[] {})
.setHash(hash)
.build();
- Since:
- 0.1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceBuffer like use it to increase rendering time while using helpers.static classAnOptionsbuilder.static classAStringBuilderimplementation.static classThis buffer will write into the underlying writer. -
Field Summary
FieldsModifier and TypeFieldDescriptionBlock param names.final ContextThe current context.final TemplateThe current template.final HandlebarsTheHandlebarsobject.The hash options.final StringThe name of the helper.final TemplateThe current inverse template.final Object[]The parameters.final TagTypeTheTagTypefrom where the helper was called. -
Constructor Summary
ConstructorsConstructorDescriptionOptions(Handlebars handlebars, String helperName, TagType tagType, Context context, Template fn, Template inverse, Object[] params, Map<String, Object> hash, List<String> blockParams) Creates a new HandlebarsOptions.Options(Handlebars handlebars, String helperName, TagType tagType, Context context, Template fn, Template inverse, Object[] params, Map<String, Object> hash, List<String> blockParams, Writer writer) Creates a new HandlebarsOptions. -
Method Summary
Modifier and TypeMethodDescriptionApply the given template to the default context.Apply the given template to the provided context.Apply the given template to the provided context.Apply the given template to the provided context.Apply the given template to the provided context.buffer()Get a Buffer which probably increase rendering time (performance).<T> TRead the attribute from the data storage.voidSet an attribute in the data storage.fn()Apply thefntemplate using the default context.Apply thefntemplate using the provided context.Apply thefntemplate using the provided context.<T> TLook for a value in the context's stack.<T> TLook for a value in the context's stack.<T> TFind a value inside thehashattributes.<T> TFind a value inside thehashattributes.inverse()Apply theinversetemplate using the default context.Apply theinversetemplate using the provided context.Apply theinversetemplate using the provided context.booleanReturns false if its argument is false, null or empty list/array (a "falsy" value).<T> Tparam(int index) Return a parameter at given index.<T> Tparam(int index, T defaultValue) Return a parameter at given index.Return a previously registered partial in the current execution context.voidStore a partial in the current execution context.propertySet(Object context) List all the properties and their values for the given object.Creates aContextfrom the given model.
-
Field Details
-
handlebars
TheHandlebarsobject. Not null. -
context
The current context. Not null. -
fn
The current template. Not null. -
inverse
The current inverse template. Not null. -
params
The parameters. Not null. -
hash
The hash options. Not null. -
tagType
TheTagTypefrom where the helper was called. -
helperName
The name of the helper. -
blockParams
Block param names.
-
-
Constructor Details
-
Options
public Options(Handlebars handlebars, String helperName, TagType tagType, Context context, Template fn, Template inverse, Object[] params, Map<String, Object> hash, List<String> blockParams) Creates a new HandlebarsOptions.- Parameters:
handlebars- The handlebars instance. Required.helperName- The name of the helper. Required.tagType- TheTagTypefrom where the helper was called.context- The current context. Required.fn- The template function. Required.inverse- The inverse template function. Required.params- The parameters. Required.hash- The optional hash. Required.blockParams- The block param names. Required.
-
Options
public Options(Handlebars handlebars, String helperName, TagType tagType, Context context, Template fn, Template inverse, Object[] params, Map<String, Object> hash, List<String> blockParams, Writer writer) Creates a new HandlebarsOptions.- Parameters:
handlebars- The handlebars instance. Required.helperName- The name of the helper. Required.tagType- TheTagTypefrom where the helper was called.context- The current context. Required.fn- The template function. Required.inverse- The inverse template function. Required.params- The parameters. Required.hash- The optional hash. Required.blockParams- The block param names. Required.writer- A writer. Optional.
-
-
Method Details
-
fn
Apply thefntemplate using the default context.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
fn
Apply thefntemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
fn
Apply thefntemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
inverse
Apply theinversetemplate using the default context.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
inverse
Apply theinversetemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
inverse
Apply theinversetemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
apply
Apply the given template to the provided context. The context stack is propagated allowing the access to the whole stack.- Parameters:
template- The template.context- The context object.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
apply
Apply the given template to the provided context. The context stack is propagated allowing the access to the whole stack.- Parameters:
template- The template.context- The context object.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
apply
public CharSequence apply(Template template, Context context, List<Object> blockParams) throws IOException Apply the given template to the provided context. The context stack is propagated allowing the access to the whole stack.- Parameters:
template- The template.context- The context object.blockParams- The block param values.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
apply
public CharSequence apply(Template template, Object context, List<Object> blockParams) throws IOException Apply the given template to the provided context. The context stack is propagated allowing the access to the whole stack.- Parameters:
template- The template.context- The context object.blockParams- The block param values.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
apply
Apply the given template to the default context. The context stack is propagated allowing the access to the whole stack.- Parameters:
template- The template.- Returns:
- The resulting text.
- Throws:
IOException- If a resource cannot be loaded.
-
param
public <T> T param(int index) Return a parameter at given index. This is analogous to:
Object param = options.params[index]The only difference is the type safe feature:
MyType param = options.param(index)- Type Parameters:
T- The runtime type.- Parameters:
index- The parameter position.- Returns:
- The parameter's value.
-
param
public <T> T param(int index, T defaultValue) Return a parameter at given index. This is analogous to:
Object param = options.params[index]The only difference is the type safe feature:
MyType param = options.param(index)- Type Parameters:
T- The runtime type.- Parameters:
index- The parameter position.defaultValue- The default value to return if the parameter is not present or if null.- Returns:
- The parameter's value.
-
get
Look for a value in the context's stack.- Type Parameters:
T- The runtime type.- Parameters:
name- The property's name.defaultValue- The default value to return if the attribute is not present or if null.- Returns:
- The associated value or
nullif it's not found.
-
get
Look for a value in the context's stack.- Type Parameters:
T- The runtime type.- Parameters:
name- The property's name.- Returns:
- The associated value or
nullif it's not found.
-
partial
Return a previously registered partial in the current execution context.- Parameters:
path- The partial's path. Required.- Returns:
- A previously registered partial in the current execution context.
Or
nullif not found.
-
partial
Store a partial in the current execution context.- Parameters:
path- The partial's path. Required.partial- The partial template. Required.
-
hash
Find a value inside the
hashattributes. This is analogous to:Object myClass = options.hash.get("class");This method works as a shorthand and type safe call:
String myClass = options.hash("class");- Type Parameters:
T- The runtime type.- Parameters:
name- The hash's name.- Returns:
- The hash value or null.
-
hash
Find a value inside the
hashattributes. This is analogous to:Object myClass = options.hash.get("class");This method works as a shorthand and type safe call:
String myClass = options.hash("class");- Type Parameters:
T- The runtime type.- Parameters:
name- The hash's name.defaultValue- The default value to returns.- Returns:
- The hash value or null.
-
isFalsy
Returns false if its argument is false, null or empty list/array (a "falsy" value).- Parameters:
value- A value.- Returns:
- False if its argument is false, null or empty list/array (a "falsy" value).
-
wrap
Creates aContextfrom the given model. If the object is a context already the same object will be returned.- Parameters:
model- The model object.- Returns:
- A context representing the model or the same model if it's a context already.
-
data
Read the attribute from the data storage.- Type Parameters:
T- Data type.- Parameters:
name- The attribute's name.- Returns:
- The attribute value or null.
-
data
Set an attribute in the data storage.- Parameters:
name- The attribute's name. Required.value- The attribute's value. Required.
-
propertySet
List all the properties and their values for the given object.- Parameters:
context- The context object. Not null.- Returns:
- All the properties and their values for the given object.
-
buffer
Get a Buffer which probably increase rendering time (performance). Usage:public CharSequence helper(Object ctx, Options options) { Buffer buffer = options.buffer(); ... buffer.append(...); ... return buffer; }Something to keep in mind is that when using the native buffer there won't be any visible output. For exampleOptions.NativeBuffer.toString()results in an empty string, that's expected because the content is written directly to the underlying writer.- Returns:
- A new buffer.
-