Context
This is an old version of the documentation for Flowable until version 3.13 and for the Angular-based Flowable Design 3.14/3.15. If you are running the latest version of Flowable please check out the current version of this page.
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.
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
valueOrDefault
Object 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.
Example:
<<[context.valueOrDefault("${location}", "Switzerland")]>>
value
Object 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 Example:
valueOrDefault(String, Object)
method which is resolving the error case to a default value.
<<[context.value("${location}")]>>
booleanValue
boolean booleanValue(String expressionText)
Gets a boolean value from an expression.
This is useful for comparison.
The expression can be either a direct boolean variable or a comparison based on one or multiple input parameters.
<<if [context.booleanValue("${validData}")]>>Valid<<else>>Invalid<</if>>
hasValue
boolean hasValue(Object obj, String key)
To check if a property exists, use a simple property name.
To check nested values, use the If a key is provided that cannot be navigated within the provided Example usages:
.
notation, e.g., address.street
.
obj
, it will return false
for simple keys (without the .
notation) to maintain compatibility.
However, for more complex keys, an exception is thrown.
<<[context.hasValue(context.value("${person}"), "address")]>>
<<[context.hasValue(context.value("${person}"), "address.street")]>>
<<[context.hasValue(context.value("${person}"), "unknown")]>> // returns false
<<[context.hasValue(context.value("${person}"), "unknown.nested")]>> // throws error
mapValue
Object mapValue(Object obj, String key)
To retrieve a property, use a simple property name.
To retrieve nested values, use the If a key is provided that cannot be navigated within the provided Example usages:
.
notation, e.g., address.street
.
obj
, it will return null
for simple keys (without the .
notation) to maintain compatibility.
However, for more complex keys, an exception is thrown.
<<[context.mapValue(context.value("${person}"), "address")]>>
<<[context.mapValue(context.value("${person}"), "address.street")]>>
<<[context.mapValue(context.value("${person}"), "unknown")]>> // returns null
<<[context.mapValue(context.value("${person}"), "unknown.nested")]>> // throws error
evaluate
Object evaluate(String expression, Object item)
Evaluates a given expression using a specified input parameter.
It takes exactly one Example:
This functionality can be helpful when applied within a loop, allowing for dynamic evaluation of expressions based on the current item
which is either a Map
, a VariableContainer
or a JsonNode
.
The item is 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.
<<[context.evaluate("${firstName}", person)]>>
item
.
Object evaluate(String expression, Object item, String itemName)
Evaluates a given expression using a specified input parameter.
This method accepts any type of The provided Example:
This functionality can be helpful when applied within a loop, allowing for dynamic evaluation of expressions based on the current item
, which is identified by an itemName
.
This itemName
provides a way to reference the input item
within the expression.
The expression has access to all variables present in the document's context.
item
is accessible via its itemName
, enabling it to serve as an argument for method invocations within the expression.
This feature facilitates the production of calculated results.
<<[context.evaluate("${personObj.firstName}", person, "personObj")]>>
<<[context.evaluate("${exampleService.getSalutation(personObj)}", person, "personObj")]>>
item
.
The advantage of this approach is that it does not require the item
to conform to any specific type, while the given item
can be fully utilized within expressions.
foreach
LinqIterable foreach(String expressionText)
Ability to iterate over a list based on an expression which evaluates to an iterable item.
This could be a list coming from a form or created with an Init Variable task or something else.
Example:
We're very happy to inform you that your order is ready for shipping:
<<foreach [item in context.foreach("${items}")]>>
- <<[context.mapValue(item,"name")]>>, with a business value of <<[context.mapValue(item, "value")]>>
<</foreach>>
Iterable foreach(Object object)
Ability to iterate over a list based on an iterable object.
This could be a list coming from a form or created with an Init Variable task or something else.
Example:
We're very happy to inform you that your order is ready for shipping:
<<foreach [item in context.foreach(context.mapValue("${businessData}", "items"))]>>
- <<[context.mapValue(item,"name")]>>, with a business value of <<[context.mapValue(item, "value")]>>
<</foreach>>
image
byte[] image(String expressionText)
Displays an image within a document
Example:
<<image [context.image("${anImage}")] -fitWidth>>
asHtml
String asHtml(String expressionText)
The 'rich text' component stores its value either as markdown or html.
When stored as markdown, it's possible to transform this to html using
the When stored as html, the html won't be 100% perfect for using in Aspose
with a Word template. Calling this method will change the html that is
incompatible with using it in such a template.
Note that it's always possible to get the 'raw html' by simply
calling the Example:
markdownAsHtml(String)
function'.
value(String)
method like any other component.
<<[context.asHtml("${unfilteredHtml}")] -html>>
markdownAsHtml
String markdownAsHtml(String expressionText)
Converts text as a markdown to a HTML text which can be used for rendering with Aspose.
It is useful when you want to display the result of a Rich text editor within an Aspose document.
Example:
<<[context.markdownAsHtml("${myRichText}")] -html>>