Class Context

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

public class Context extends Object
Mustache/Handlebars are contextual template engines. This class represent the 'context stack' of a template.
  • Objects and hashes should be pushed onto the context stack.
  • All elements on the context stack should be accessible.
  • Multiple sections per template should be permitted.
  • Failed context lookups should be considered falsy.
  • Dotted names should be valid for Section tags.
  • Dotted names that cannot be resolved should be considered falsy.
  • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
Since:
0.1.0
  • Field Details

    • PARTIALS

      public static final String PARTIALS
      The qualified name for partials. Internal use.
    • INLINE_PARTIALS

      public static final String INLINE_PARTIALS
      Inline partials.
      See Also:
    • INVOCATION_STACK

      public static final String INVOCATION_STACK
      The qualified name for partials. Internal use.
    • PARAM_SIZE

      public static final String PARAM_SIZE
      Number of parameters of a helper. Internal use.
    • CALLEE

      public static final String CALLEE
      Last callee of a partial block. Internal use.
  • Method Details

    • combine

      public Context combine(String name, Object model)
      Insert a new attribute in the context-stack.
      Parameters:
      name - The attribute's name. Required.
      model - The model data.
      Returns:
      This context.
    • combine

      public Context combine(Map<String,?> model)
      Insert all the attributes in the context-stack.
      Parameters:
      model - The model attributes.
      Returns:
      This context.
    • 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 Context 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.
      Returns:
      This context.
    • data

      public Context data(Map<String,?> attributes)
      Store the map in the data storage.
      Parameters:
      attributes - The attributes to add. Required.
      Returns:
      This context.
    • model

      public final Object model()
      Resolved as '.' or 'this' inside templates.
      Returns:
      The model or data.
    • parent

      public final Context parent()
      The parent context or null.
      Returns:
      The parent context or null.
    • propertySet

      public Set<Map.Entry<String,Object>> propertySet(Object context)
      List all the properties and values for the given object.
      Parameters:
      context - The context object.
      Returns:
      All the properties and values for the given object.
    • propertySet

      public Set<Map.Entry<String,Object>> propertySet()
      List all the properties and values of model().
      Returns:
      All the properties and values of model().
    • isBlockParams

      public boolean isBlockParams()
      Returns:
      True, if this context is a block param context.
    • get

      public Object get(List<PathExpression> path)
      Lookup the given key inside the context stack.
      • Objects and hashes should be pushed onto the context stack.
      • All elements on the context stack should be accessible.
      • Multiple sections per template should be permitted.
      • Failed context lookups should be considered falsey.
      • Dotted names should be valid for Section tags.
      • Dotted names that cannot be resolved should be considered falsey.
      • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
      Parameters:
      path - The object path.
      Returns:
      The value associated to the given key or null if no value is found.
    • get

      public Object get(String key)
      Lookup the given key inside the context stack.
      • Objects and hashes should be pushed onto the context stack.
      • All elements on the context stack should be accessible.
      • Multiple sections per template should be permitted.
      • Failed context lookups should be considered falsey.
      • Dotted names should be valid for Section tags.
      • Dotted names that cannot be resolved should be considered falsey.
      • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
      Parameters:
      key - The object key.
      Returns:
      The value associated to the given key or null if no value is found.
    • get

      public Object get(String key, boolean parentScopeResolution)
      Lookup the given key inside the context stack.
      • Objects and hashes should be pushed onto the context stack.
      • All elements on the context stack should be accessible.
      • Multiple sections per template should be permitted.
      • Failed context lookups should be considered falsey.
      • Dotted names should be valid for Section tags.
      • Dotted names that cannot be resolved should be considered falsey.
      • Dotted Names - Context Precedence: Dotted names should be resolved against former resolutions.
      Parameters:
      key - The object key.
      parentScopeResolution - False, if we want to restrict lookup to current scope.
      Returns:
      The value associated to the given key or null if no value is found.
    • destroy

      public void destroy()
      Destroy this context by cleaning up instance attributes.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • newBuilder

      public static Context.Builder newBuilder(Context parent, Object model)
      Start a new context builder.
      Parameters:
      parent - The parent context. Required.
      model - The model data.
      Returns:
      A new context builder.
    • newBuilder

      public static Context.Builder newBuilder(Object model)
      Start a new context builder.
      Parameters:
      model - The model data.
      Returns:
      A new context builder.
    • newContext

      public static Context newContext(Context parent, Object model)
      Creates a new child context.
      Parameters:
      parent - The parent context. Required.
      model - The model data.
      Returns:
      A new child context.
    • newBlockParamContext

      public static Context newBlockParamContext(Context parent, List<String> names, List<Object> values)
      Creates a new block param context.
      Parameters:
      parent - The parent context. Required.
      names - A list of names to set in the block param context.
      values - A list of values to set in the block param context.
      Returns:
      A new block param context.
    • newPartialContext

      public static Context newPartialContext(Context ctx, String scope, Map<String,Object> hash)
      Creates a new partial context.
      Parameters:
      ctx - Current scope.
      scope - Scope switch.
      hash - Partial hash.
      Returns:
      A new context.
    • newContext

      public static Context newContext(Object model)
      Creates a new root context.
      Parameters:
      model - The model data.
      Returns:
      A new root context.
    • copy

      public static Context copy(Context context, Object model)
      Creates a new context but keep the data attribute.
      Parameters:
      context - Context to extract the data attribute.
      model - A model/data.
      Returns:
      A new context.