Package com.github.jknack.handlebars
Class Options
- java.lang.Object
-
- com.github.jknack.handlebars.Options
-
public class Options extends java.lang.Object
Options 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 interface
Options.Buffer
Buffer like use it to increase rendering time while using helpers.static class
Options.Builder
AnOptions
builder.static class
Options.InMemoryBuffer
AStringBuilder
implementation.static class
Options.NativeBuffer
This buffer will write into the underlying writer.
-
Field Summary
Fields Modifier and Type Field Description java.util.List<java.lang.String>
blockParams
Block param names.Context
context
The current context.Template
fn
The current template.Handlebars
handlebars
TheHandlebars
object.java.util.Map<java.lang.String,java.lang.Object>
hash
The hash options.java.lang.String
helperName
The name of the helper.Template
inverse
The current inverse template.java.lang.Object[]
params
The parameters.TagType
tagType
TheTagType
from 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.CharSequence
apply(Template template)
Apply the given template to the default context.java.lang.CharSequence
apply(Template template, Context context)
Apply the given template to the provided context.java.lang.CharSequence
apply(Template template, Context context, java.util.List<java.lang.Object> blockParams)
Apply the given template to the provided context.java.lang.CharSequence
apply(Template template, java.lang.Object context)
Apply the given template to the provided context.java.lang.CharSequence
apply(Template template, java.lang.Object context, java.util.List<java.lang.Object> blockParams)
Apply the given template to the provided context.Options.Buffer
buffer()
Get a Buffer which probably increase rendering time (performance).<T> T
data(java.lang.String name)
Read the attribute from the data storage.void
data(java.lang.String name, java.lang.Object value)
Set an attribute in the data storage.java.lang.CharSequence
fn()
Apply thefn
template using the default context.java.lang.CharSequence
fn(Context context)
Apply thefn
template using the provided context.java.lang.CharSequence
fn(java.lang.Object context)
Apply thefn
template using the provided context.<T> T
get(java.lang.String name)
Look for a value in the context's stack.<T> T
get(java.lang.String name, T defaultValue)
Look for a value in the context's stack.<T> T
hash(java.lang.String name)
Find a value inside thehash
attributes.<T> T
hash(java.lang.String name, java.lang.Object defaultValue)
Find a value inside thehash
attributes.java.lang.CharSequence
inverse()
Apply theinverse
template using the default context.java.lang.CharSequence
inverse(Context context)
Apply theinverse
template using the provided context.java.lang.CharSequence
inverse(java.lang.Object context)
Apply theinverse
template using the provided context.boolean
isFalsy(java.lang.Object value)
Returns false if its argument is false, null or empty list/array (a "falsy" value).<T> T
param(int index)
Return a parameter at given index.<T> T
param(int index, T defaultValue)
Return a parameter at given index.Template
partial(java.lang.String path)
Return a previously registered partial in the current execution context.void
partial(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.Context
wrap(java.lang.Object model)
Creates aContext
from the given model.
-
-
-
Field Detail
-
handlebars
public final Handlebars handlebars
TheHandlebars
object. 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
- TheTagType
from 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
- TheTagType
from 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.IOException
Apply thefn
template 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.IOException
Apply thefn
template 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 thefn
template 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.IOException
Apply theinverse
template 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.IOException
Apply theinverse
template 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 theinverse
template 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
null
if 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
null
if 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
null
if 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
hash
attributes. 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
hash
attributes. 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 aContext
from 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.
-
-