Class Options

java.lang.Object
com.github.jknack.handlebars.Options

public class Options extends Object
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
  • Field Details

    • handlebars

      public final Handlebars handlebars
      The Handlebars 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 Object[] params
      The parameters. Not null.
    • hash

      public final Map<String,Object> hash
      The hash options. Not null.
    • tagType

      public final TagType tagType
      The TagType from where the helper was called.
    • helperName

      public final String helperName
      The name of the helper.
    • blockParams

      public final List<String> 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 Handlebars Options.
      Parameters:
      handlebars - The handlebars instance. Required.
      helperName - The name of the helper. Required.
      tagType - The TagType 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, String helperName, TagType tagType, Context context, Template fn, Template inverse, Object[] params, Map<String,Object> hash, List<String> blockParams, Writer writer)
      Creates a new Handlebars Options.
      Parameters:
      handlebars - The handlebars instance. Required.
      helperName - The name of the helper. Required.
      tagType - The TagType 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 Details

    • fn

      public CharSequence fn() throws IOException
      Apply the fn template using the default context.
      Returns:
      The resulting text.
      Throws:
      IOException - If a resource cannot be loaded.
    • fn

      public CharSequence fn(Object context) throws IOException
      Apply the fn template using the provided context.
      Parameters:
      context - The context to use.
      Returns:
      The resulting text.
      Throws:
      IOException - If a resource cannot be loaded.
    • fn

      public CharSequence fn(Context context) throws IOException
      Apply the fn template using the provided context.
      Parameters:
      context - The context to use.
      Returns:
      The resulting text.
      Throws:
      IOException - If a resource cannot be loaded.
    • inverse

      public CharSequence inverse() throws IOException
      Apply the inverse template using the default context.
      Returns:
      The resulting text.
      Throws:
      IOException - If a resource cannot be loaded.
    • inverse

      public CharSequence inverse(Object context) throws IOException
      Apply the inverse template using the provided context.
      Parameters:
      context - The context to use.
      Returns:
      The resulting text.
      Throws:
      IOException - If a resource cannot be loaded.
    • inverse

      public CharSequence inverse(Context context) throws IOException
      Apply the inverse template using the provided context.
      Parameters:
      context - The context to use.
      Returns:
      The resulting text.
      Throws:
      IOException - If a resource cannot be loaded.
    • apply

      public CharSequence apply(Template template, Object context) 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.
      Returns:
      The resulting text.
      Throws:
      IOException - If a resource cannot be loaded.
    • apply

      public CharSequence apply(Template template, Context context) 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.
      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

      public CharSequence apply(Template template) throws 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:
      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(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(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(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(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(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(String name, 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(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(Object model)
      Creates a Context 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(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(String name, Object value)
      Set an attribute in the data storage.
      Parameters:
      name - The attribute's name. Required.
      value - The attribute's value. Required.
    • propertySet

      public Set<Map.Entry<String,Object>> propertySet(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 example Options.NativeBuffer.toString() results in an empty string, that's expected because the content is written directly to the underlying writer.
      Returns:
      A new buffer.