Package com.github.jknack.handlebars
Class Context
- java.lang.Object
-
- com.github.jknack.handlebars.Context
-
public class Context extends java.lang.ObjectMustache/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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classContext.BuilderA context builder.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCALLEELast callee of a partial block.static java.lang.StringINLINE_PARTIALSInline partials.static java.lang.StringINVOCATION_STACKThe qualified name for partials.static java.lang.StringPARAM_SIZENumber of parameters of a helper.static java.lang.StringPARTIALSThe qualified name for partials.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Contextcombine(java.lang.String name, java.lang.Object model)Insert a new attribute in the context-stack.Contextcombine(java.util.Map<java.lang.String,?> model)Insert all the attributes in the context-stack.static Contextcopy(Context context, java.lang.Object model)Creates a new context but keep thedataattribute.<T> Tdata(java.lang.String name)Read the attribute from the data storage.Contextdata(java.lang.String name, java.lang.Object value)Set an attribute in the data storage.Contextdata(java.util.Map<java.lang.String,?> attributes)Store the map in the data storage.voiddestroy()Destroy this context by cleaning up instance attributes.java.lang.Objectget(java.lang.String key)Lookup the given key inside the context stack.java.lang.Objectget(java.lang.String key, boolean parentScopeResolution)Lookup the given key inside the context stack.java.lang.Objectget(java.util.List<PathExpression> path)Lookup the given key inside the context stack.booleanisBlockParams()java.lang.Objectmodel()Resolved as '.' or 'this' inside templates.static ContextnewBlockParamContext(Context parent, java.util.List<java.lang.String> names, java.util.List<java.lang.Object> values)Creates a new block param context.static Context.BuildernewBuilder(Context parent, java.lang.Object model)Start a new context builder.static Context.BuildernewBuilder(java.lang.Object model)Start a new context builder.static ContextnewContext(Context parent, java.lang.Object model)Creates a new child context.static ContextnewContext(java.lang.Object model)Creates a new root context.static ContextnewPartialContext(Context ctx, java.lang.String scope, java.util.Map<java.lang.String,java.lang.Object> hash)Creates a new partial context.Contextparent()The parent context or null.java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>propertySet()List all the properties and values ofmodel().java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>propertySet(java.lang.Object context)List all the properties and values for the given object.java.lang.StringtoString()
-
-
-
Field Detail
-
PARTIALS
public static final java.lang.String PARTIALS
The qualified name for partials. Internal use.
-
INLINE_PARTIALS
public static final java.lang.String INLINE_PARTIALS
Inline partials.- See Also:
- Constant Field Values
-
INVOCATION_STACK
public static final java.lang.String INVOCATION_STACK
The qualified name for partials. Internal use.
-
PARAM_SIZE
public static final java.lang.String PARAM_SIZE
Number of parameters of a helper. Internal use.
-
CALLEE
public static final java.lang.String CALLEE
Last callee of a partial block. Internal use.
-
-
Method Detail
-
combine
public Context combine(java.lang.String name, java.lang.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(java.util.Map<java.lang.String,?> model)
Insert all the attributes in the context-stack.- Parameters:
model- The model attributes.- Returns:
- This context.
-
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 Context 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.- Returns:
- This context.
-
data
public Context data(java.util.Map<java.lang.String,?> attributes)
Store the map in the data storage.- Parameters:
attributes- The attributes to add. Required.- Returns:
- This context.
-
model
public final java.lang.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 java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> propertySet(java.lang.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 java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> propertySet()
List all the properties and values ofmodel().- Returns:
- All the properties and values of
model().
-
isBlockParams
public boolean isBlockParams()
- Returns:
- True, if this context is a block param context.
-
get
public java.lang.Object get(java.util.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
nullif no value is found.
-
get
public java.lang.Object get(java.lang.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
nullif no value is found.
-
get
public java.lang.Object get(java.lang.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
nullif no value is found.
-
destroy
public void destroy()
Destroy this context by cleaning up instance attributes.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
newBuilder
public static Context.Builder newBuilder(Context parent, java.lang.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(java.lang.Object model)
Start a new context builder.- Parameters:
model- The model data.- Returns:
- A new context builder.
-
newContext
public static Context newContext(Context parent, java.lang.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, java.util.List<java.lang.String> names, java.util.List<java.lang.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, java.lang.String scope, java.util.Map<java.lang.String,java.lang.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(java.lang.Object model)
Creates a new root context.- Parameters:
model- The model data.- Returns:
- A new root context.
-
-