Context
The context is used within Word document templates and is available through the keyword context
.
It is a supporting layer between Aspose and Flowable to use Flowable expressions and variables during the template generation. Below there is a list of methods which can be used.
tip
The name of the context object can be changed by the Context object property in Flowable Design. The default value is context.
To get started with the template generation, checkout the how-to generate documents.
Methods
Value retrieval
hasValue(Object obj, String key)
: Checks if a (nested) value from a map, JSON node, or variable container exists and returns a boolean value. Examples:<<[context.hasValue(context.value("${person}"), "address")]>>
,<<[context.hasValue(context.value("${person}"), "address.street")]>>
value(String expressionText)
: Gets a value from an expression. In case an expression is unable to resolve it will throw an error, and it won't generate the document. This can be useful if the document should not be rendered in case of errors in the expression. However, an alternative approach is to use the{@link #
valueOrDefault(String, Object)method which is resolving the error case to a default value. Example:
[context.value("${location}")]`valueOrDefault(String expressionText, Object defaultValue)
: Gets a value from an expression or returns a default value in case the value resolves to null or the expression throws an exception. For example, when the value could not be found it will render the default value.booleanValue(String expressionText)
: Gets a boolean value from an expression.mapValue(Object obj, String key)
: Retrieves a (nested) value from a map, JSON node, or variable container. Examples:<<[context.mapValue(context.value("${person}"), "address")]>>
,<<[context.mapValue(context.value("${person}"), "address.street")]>>
,<<[context.mapValue(context.value("${person}"), "unknown")]>>
General expression evaluation
evaluate(String expression, Object item)
: Evaluates a given expression using a specified input parameter. It takes exactly oneitem
which is either a Map, a VariableContainer or a JsonNode. The item is then used as a base for an expression. The expression directly uses the field out of the object which is provided and does not require an additional prefix. The expression has access to all variables present in the document's context.evaluate(String expression, Object item, String itemName)
: This method accepts any type ofitem
, which is identified by anitemName
. ThisitemName
provides a way to reference the inputitem
within the expression.
Looping
foreach(String expressionText)
: Ability to iterate over a list based on an expression which evaluates to an iterable item. This could for exmaple be a list coming from a form or created with an Init Variable task or something else.
Images
image(String expressionText)
: Displays an image within a document. The expression needs to resolve to a content item, a list of content items or bytes.
HTML and Markdown
asHtml(String expressionText)
: The 'rich text' form component stores its value either as markdown or html. When stored as html, the html won't be 100% perfect for using in Aspose within a Word template. Calling this method will change the html that is incompatible to make it compatible.markdownAsHtml(String expressionText)
: Converts text as a markdown to a HTML text which can be used for rendering within a Word template.