Package com.github.jknack.handlebars
Class Options
- java.lang.Object
-
- com.github.jknack.handlebars.Options
-
public class Options extends java.lang.ObjectOptions available forHelper.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 Classes Modifier and Type Class Description static interfaceOptions.BufferBuffer like use it to increase rendering time while using helpers.static classOptions.BuilderAnOptionsbuilder.static classOptions.InMemoryBufferAStringBuilderimplementation.static classOptions.NativeBufferThis buffer will write into the underlying writer.
-
Field Summary
Fields Modifier and Type Field Description java.util.List<java.lang.String>blockParamsBlock param names.ContextcontextThe current context.TemplatefnThe current template.HandlebarshandlebarsTheHandlebarsobject.java.util.Map<java.lang.String,java.lang.Object>hashThe hash options.java.lang.StringhelperNameThe name of the helper.TemplateinverseThe current inverse template.java.lang.Object[]paramsThe parameters.TagTypetagTypeTheTagTypefrom where the helper was called.
-
Constructor Summary
Constructors Constructor Description Options(Handlebars handlebars, java.lang.String helperName, TagType tagType, Context context, Template fn, Template inverse, java.lang.Object[] params, java.util.Map<java.lang.String,java.lang.Object> hash, java.util.List<java.lang.String> blockParams)Creates a new HandlebarsOptions.Options(Handlebars handlebars, java.lang.String helperName, TagType tagType, Context context, Template fn, Template inverse, java.lang.Object[] params, java.util.Map<java.lang.String,java.lang.Object> hash, java.util.List<java.lang.String> blockParams, java.io.Writer writer)Creates a new HandlebarsOptions.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.CharSequenceapply(Template template)Apply the given template to the default context.java.lang.CharSequenceapply(Template template, Context context)Apply the given template to the provided context.java.lang.CharSequenceapply(Template template, Context context, java.util.List<java.lang.Object> blockParams)Apply the given template to the provided context.java.lang.CharSequenceapply(Template template, java.lang.Object context)Apply the given template to the provided context.java.lang.CharSequenceapply(Template template, java.lang.Object context, java.util.List<java.lang.Object> blockParams)Apply the given template to the provided context.Options.Bufferbuffer()Get a Buffer which probably increase rendering time (performance).<T> Tdata(java.lang.String name)Read the attribute from the data storage.voiddata(java.lang.String name, java.lang.Object value)Set an attribute in the data storage.java.lang.CharSequencefn()Apply thefntemplate using the default context.java.lang.CharSequencefn(Context context)Apply thefntemplate using the provided context.java.lang.CharSequencefn(java.lang.Object context)Apply thefntemplate using the provided context.<T> Tget(java.lang.String name)Look for a value in the context's stack.<T> Tget(java.lang.String name, T defaultValue)Look for a value in the context's stack.<T> Thash(java.lang.String name)Find a value inside thehashattributes.<T> Thash(java.lang.String name, java.lang.Object defaultValue)Find a value inside thehashattributes.java.lang.CharSequenceinverse()Apply theinversetemplate using the default context.java.lang.CharSequenceinverse(Context context)Apply theinversetemplate using the provided context.java.lang.CharSequenceinverse(java.lang.Object context)Apply theinversetemplate using the provided context.booleanisFalsy(java.lang.Object value)Returns 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.Templatepartial(java.lang.String path)Return a previously registered partial in the current execution context.voidpartial(java.lang.String path, Template partial)Store a partial in the current execution context.java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>propertySet(java.lang.Object context)List all the properties and their values for the given object.Contextwrap(java.lang.Object model)Creates aContextfrom the given model.
-
-
-
Field Detail
-
handlebars
public final Handlebars handlebars
TheHandlebarsobject. Not null.
-
context
public final Context context
The current context. Not null.
-
fn
public final Template fn
The current template. Not null.
-
inverse
public final Template inverse
The current inverse template. Not null.
-
params
public final java.lang.Object[] params
The parameters. Not null.
-
hash
public final java.util.Map<java.lang.String,java.lang.Object> hash
The hash options. Not null.
-
helperName
public final java.lang.String helperName
The name of the helper.
-
blockParams
public final java.util.List<java.lang.String> blockParams
Block param names.
-
-
Constructor Detail
-
Options
public Options(Handlebars handlebars, java.lang.String helperName, TagType tagType, Context context, Template fn, Template inverse, java.lang.Object[] params, java.util.Map<java.lang.String,java.lang.Object> hash, java.util.List<java.lang.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, java.lang.String helperName, TagType tagType, Context context, Template fn, Template inverse, java.lang.Object[] params, java.util.Map<java.lang.String,java.lang.Object> hash, java.util.List<java.lang.String> blockParams, java.io.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 Detail
-
fn
public java.lang.CharSequence fn() throws java.io.IOExceptionApply thefntemplate using the default context.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
fn
public java.lang.CharSequence fn(java.lang.Object context) throws java.io.IOExceptionApply thefntemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
fn
public java.lang.CharSequence fn(Context context) throws java.io.IOException
Apply thefntemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
inverse
public java.lang.CharSequence inverse() throws java.io.IOExceptionApply theinversetemplate using the default context.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
inverse
public java.lang.CharSequence inverse(java.lang.Object context) throws java.io.IOExceptionApply theinversetemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
inverse
public java.lang.CharSequence inverse(Context context) throws java.io.IOException
Apply theinversetemplate using the provided context.- Parameters:
context- The context to use.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
apply
public java.lang.CharSequence apply(Template template, java.lang.Object context) throws java.io.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.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
apply
public java.lang.CharSequence apply(Template template, Context context) throws java.io.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.- Returns:
- The resulting text.
- Throws:
java.io.IOException- If a resource cannot be loaded.
-
apply
public java.lang.CharSequence apply(Template template, Context context, java.util.List<java.lang.Object> blockParams) throws java.io.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:
java.io.IOException- If a resource cannot be loaded.
-
apply
public java.lang.CharSequence apply(Template template, java.lang.Object context, java.util.List<java.lang.Object> blockParams) throws java.io.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:
java.io.IOException- If a resource cannot be loaded.
-
apply
public java.lang.CharSequence apply(Template template) throws java.io.IOException
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:
java.io.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
public <T> T get(java.lang.String name, T defaultValue)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
public <T> T get(java.lang.String name)
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
public Template partial(java.lang.String path)
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
public void partial(java.lang.String path, Template partial)Store a partial in the current execution context.- Parameters:
path- The partial's path. Required.partial- The partial template. Required.
-
hash
public <T> T hash(java.lang.String name)
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
public <T> T hash(java.lang.String name, java.lang.Object defaultValue)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
public boolean isFalsy(java.lang.Object value)
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
public Context wrap(java.lang.Object model)
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
public <T> T data(java.lang.String name)
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
public void data(java.lang.String name, java.lang.Object value)Set an attribute in the data storage.- Parameters:
name- The attribute's name. Required.value- The attribute's value. Required.
-
propertySet
public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> propertySet(java.lang.Object context)
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
public Options.Buffer 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.
-
-